Android复用title布局_01

应用场景:

在Android开发中有很多页面的标题栏都是一样的格式,那么我们就没有必要每次都写一大堆布局,可以此阿勇简化的方法,现在就来介绍这种方法的使用步骤。

应用方法

首先根据自己需要的样式写出一个布局,如下名字叫做title_common_ui(无所谓,随便起):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#ffffff"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv_left"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:background="@mipmap/back" />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="标题"
        android:textSize="18sp" />

    <Button
        android:id="@+id/btn_right"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:text="右边按键"
        android:visibility="invisible" />

</RelativeLayout>

怎么使用呢:

在布局文件中直接使用include引用,如下:

<include layout="@layout/title_common_ui" />

怎么赋值,设置监听等等呢:

在代码中查找到控件,然后就可以进行你想要的操作了,无论是设置点击事件,赋值,隐藏与显示,颜色等等,例如:

 ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        Button btn_right = (Button) findViewById(R.id.btn_right);

        //对这些控件进行各种操作
        iv_left.setOnClickListener(null);
        tv_title.setText("我的标题");
        btn_right.setVisibility(View.VISIBLE);

注:这种方法当然也存在不方便的地方就是属性不能在布局文件中直接设置了,都需要通过代码了,可能对于一些同学就会不习惯了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值