android UI(2)自定义控件简单使用

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  android:orientation="vertical" >
	<include
    android:id="@+id/title"
    layout="@layout/title"
    />
    <TextView
        android:textSize="15sp"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这个是首页" />

    <com.example.demo1.BottomTitle
        android:id="@+id/buttomtitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         >
    </com.example.demo1.BottomTitle>

</LinearLayout>



在这个 主activity的布局文件中  有两个标签 include和 com.example.demo1.BottomTitle


如果是要把一个简单的布局文件变成一个view  只需要用include标签导入这个layout文件


如果是一个自定义控件(有自己实现的功能比如点击就变色或者切换fragment等等) 可以new一个class 继承控件或者布局  在他的构造方法中描绘这个自定义控件

public class BottomTitle extends LinearLayout {

	public BottomTitle(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
	}

	public BottomTitle(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		setOrientation(LinearLayout.HORIZONTAL);
	Button button1  = new Button(getContext());
	button1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
	button1.setText("首页");
	addView(button1);
	Button button2  = new Button(getContext());
	button2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
	button2.setText("帮助");
	addView(button2);
	}
   
}

参考文章  :      点击打开链接



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值