android#嵌入式布局并创建自定义控件
2024-09-03 17:12:32
一、如何在android中嵌入布局文件:
新建一个布局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:id="@+id/title_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dip" android:background="@drawable/back_bg" android:text="Back" android:textColor="#fff" /> <TextView android:id="@+id/title_text" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="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 128 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>
我们在主页面中添加如下代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <include layout="@layout/title" /> </LinearLayout>
即可引入公共布局文件
二、如何创建自定义控件
在以上代码的基础上新建如下代码:
public class TitleLayout extends LinearLayout { public TitleLayout(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.title, this); } }
并将主页面代码修改为
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.uicustomviews.TitleLayout android:layout_width="match_parent" android:layout_height="wrap_content" ></com.example.uicustomviews.TitleLayout> </LinearLayout>
将自定义控件的代码进行修改
public class TitleLayout extends LinearLayout { public TitleLayout(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.title, 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_SHORT).show(); } }); } }
这样,在每次引入自定义控件自后,对应的按钮事件绑定就已经完成,省去编写重复代码。
最新文章
- Favorites of top 10 rules for success
- Oracle体系中各个组件的含义
- phpcms 完美实现 导航栏当前栏目高亮
- JAVA中implements的用法
- 属性通知之INotifyPropertyChanged
- mysql自增
- 高级特性(2)- XML
- redis在spring-boot中的应用
- Linux 学习记录 一(安装、基本文件操作).
- [UOJ]#36. 【清华集训2014】玛里苟斯
- thinkPHP5.0 URL路由优化
- 《SQL优化入门》讲座总结
- Redis扩展机制
- [c/c++] programming之路(24)、字符串(五)——字符串插入,字符串转整数,删除字符,密码验证,注意事项
- [Aaronyang] 写给自己的WPF4.5 笔记16[多线程]
- Jacky扯淡系列 – 验证码
- JDK源码阅读顺序
- LG2044 [NOI2012]随机数生成器
- Spring 注解方式 实现 IOC 和 DI
- 在客户端模拟调用srv和topic