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

来源:互联网 发布:软件版权修改器 编辑:程序博客网 时间:2024/05/19 23:26
<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 stubsetOrientation(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
原创粉丝点击