首页 > 系统 > Android > 正文

Android复用title布局

2019-11-09 18:10:56
字体:
来源:转载
供稿:网友

应用场景:

在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);

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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表