创建自定义控件

控件和布局的继承结构



控件都继承自View 布局都是继承自Viewgroup
当自带控件不能满足我们的需求时,就要开始创建自定义控件啦
3.4.1 引入布局
新建布局title.xml,代码如下所示
   
   
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_bg" >
 
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title_back"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/back_bg"
android:text="Back"
android:textColor="#fff" />
 
<TextView
android:id="@+id/title_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_wight="1"
android:gravity="center"
android:text="Title Text"
android:textColor="#fff"
android:textSize="24sp" />
 
<Button
android:id="@+id/title_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:background="@drawable/edit_bg"
android:text="Edit"
android:textColor="#fff" />
</LinearLayout>
其中
   
   
android:layout_margin="5dip"
含义为指定空间可以再上下左右方向上偏移的距离
也可以利用
   
   
android:layout_marginTop="5dip"
等单独对某个方向上的距离的进行设置

如何在程序中使用这个标题栏呢 在activity_main.xml中添加如下代码
   
   
<include layout="@layout/title_activity" />
最后别忘了 隐藏原系统自带的标题栏
   
   
requestWindowFeature(Window.FEATURE_NO_TITLE);

3.4.2 创建自定义控件

为避免在每个活动中为某个控件单独编写一次事件注册的代码,可以使用自定义控件来解决这个问题
新建TitleLayout继承自LinearLayout 让它成为我们自定义的标题栏控件
新建活动TitleLayout 继承自Lineaout
代码如下
   
   
public class TitleLayout extends LinearLayout {
 
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title_activity, this);
//LayoutInflater的from()方法可以构建出一个LayoutInflater对象,然后调用inflate()方法就可以动态加载一个布局文件,inflate接受链各个参数,第一个参数是要加载的布局文件的id,这里我们传入R.layout.titleactivity,第二个参数是给加载好的布局再添加一个父布局,这里我们想要指定为TitleLayout,于是直接传入this。
}
}
然后我们需要在布局文件中标添加这个自定义类
   
   
<com.example.uicustomviews.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
></com.example.uicustomviews.TitleLayout>
//添加自定义控件和添加普通控件的方式基本上是一样的,只不过在添加自定义控件的时候我们需要指明控件的完整类名,包名在这里是不可以省略的

然后尝试为标题栏中的按钮注册点击事件,修改TitleLayout中的代码如下
   
   
 
public class TitleLayout extends LinearLayout {
 
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title_activity, this);
Button titleBack = (Button) findViewById(R.id.title_back);
Button titleEdit = (Button) findViewById(R.id.title_edit);
titleBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((Activity) getContext()).finish();
}
});
titleEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "You clicked Edit Button",
Toast.LENGTH_LONG).show();
}
});
}
}
实际模拟器如图

这样的话每当我们在一个布局中引入TitleLayout 返回按钮和编辑按钮的点击事件就已经自动实现好了,也是省去了很多编写重复代码的工作!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值