安卓自定义布局实现

来源:互联网 发布:airplayiphone连mac 编辑:程序博客网 时间:2024/05/20 03:44

因为在很多时候我们想自己控制一个View的实现,那就需要在xml文件中加入一个自己创建的VIEW。而如果是在JAVA文件中用代码画图,当然可行,但是比较繁琐,而我做的是插入自己的View,在java代码中实现这个view所要用的一些功能。

XML

写一个LinearLayout自定义View实现
下面是需要在一个布局中加入我的自定义的View
fragment_station.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="wrap_content">    <!-- 自定义的VIEW-->    <cn.tcb.destmonitoronline.view.HeaderDisplay        android:id="@+id/header"        android:layout_width="match_parent"        android:layout_height="wrap_content">    </cn.tcb.destmonitoronline.view.HeaderDisplay>    <View        android:layout_width="match_parent"        android:layout_height="1dp"        android:background="@color/light_gray"/>    <ListView        android:id="@+id/list_view_station"        android:layout_width="match_parent"        android:layout_height="match_parent">    </ListView></LinearLayout>

header_display.xml
自定义VIEW的实现:里面包含一个textview和一个imageview,而我的整个LinearLayout要实现点击功能

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="?android:attr/selectableItemBackground"   //用来显示点击效果    >    <TextView        android:id="@+id/header_text"        android:textSize="20sp"        android:gravity="center"        android:layout_weight="6"        android:layout_width="1px"        android:layout_height="?attr/listPreferredItemHeight"       />    <ImageView        android:id="@+id/header_image"        android:layout_weight="1"        android:layout_width="1px"        android:layout_height="wrap_content"        android:src="@mipmap/search"        android:layout_gravity="center"/></LinearLayout>

JAVA

自定义的View文件在此我继承的是layout,如果extends View则需要在java文件中进行画图,用java的canvas,paint等类进行画图,而我只是想要把继承一个layout以便于之后的复用,因为有多个地方用到这个我定义的标头layout,所以我直接extends layout并加载一个xml作为布局。

HeaderDisplay .java

public class HeaderDisplay extends LinearLayout{    /**     * @Description: 构造方法,多个构造方法以便使用     *     * @auther renchongbin     * created at 2016/6/1 14:27     */    public HeaderDisplay(Context context) {        super(context);        initLayout();    }    public HeaderDisplay(Context context, AttributeSet attrs) {        super(context, attrs);        initLayout();    }    public HeaderDisplay(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        initLayout();    }    //初始化变量    private TextView mTextTitle;    private ImageView mImageView;    /**     * @Description: 初始化layout和里面的控件     *     * @auther renchongbin     * created at 2016/6/1 14:27     */    protected void initLayout() {        //找出相应布局文件并加载        LayoutInflater inflater=(LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);        View layout = inflater.inflate(R.layout.header_display, this, true);//要用this来进行确定加载此布局        //获取到相应的控件id        mTextTitle = (TextView)findViewById(R.id.header_text);        mImageView = (ImageView)findViewById(R.id.header_image);    }    /**     * @Description: 设置text显示的内容     *     * @auther renchongbin     * created at 2016/6/1 14:26     */    public void setText(String name){        mTextTitle.setText(name);    }}

下面就可以在其他文件中进行调用自定义View的功能了
先声明

private HeaderDisplay mHeaderDisplay;

然后调用

 @Override    public View onCreateView(LayoutInflater inflater,                             ViewGroup container,                             Bundle savedInstanceState) {    //此fragment加载的界面        View view = inflater.inflate(R.layout.fragment_stations, container, false);        //获取Header布局控件        mHeaderDisplay = (HeaderDisplay) view.findViewById(R.id.header);        //点击响应事件        mHeaderDisplay.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {            Intent regionFilterDialogIntent = new Intent(getActivity(), RegionFilterDialog.class);                startActivityForResult(regionFilterDialogIntent, REQUEST_FOR_REGION_FILTER);            }        });
1 0
原创粉丝点击