安卓自定义布局实现

因为在很多时候我们想自己控制一个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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Charles Ray

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值