一、如何在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();
}
});
}
}

这样,在每次引入自定义控件自后,对应的按钮事件绑定就已经完成,省去编写重复代码。

最新文章

  1. Favorites of top 10 rules for success
  2. Oracle体系中各个组件的含义
  3. phpcms 完美实现 导航栏当前栏目高亮
  4. JAVA中implements的用法
  5. 属性通知之INotifyPropertyChanged
  6. mysql自增
  7. 高级特性(2)- XML
  8. redis在spring-boot中的应用
  9. Linux 学习记录 一(安装、基本文件操作).
  10. [UOJ]#36. 【清华集训2014】玛里苟斯
  11. thinkPHP5.0 URL路由优化
  12. 《SQL优化入门》讲座总结
  13. Redis扩展机制
  14. [c/c++] programming之路(24)、字符串(五)——字符串插入,字符串转整数,删除字符,密码验证,注意事项
  15. [Aaronyang] 写给自己的WPF4.5 笔记16[多线程]
  16. Jacky扯淡系列 – 验证码
  17. JDK源码阅读顺序
  18. LG2044 [NOI2012]随机数生成器
  19. Spring 注解方式 实现 IOC 和 DI
  20. 在客户端模拟调用srv和topic

热门文章

  1. NativeRenderingPlugin IOS
  2. JS用正则替换特殊字符
  3. TC做题笔记
  4. MFC 下拉框Combo Box
  5. CF732D Exams 二分 贪心
  6. 【luoguUVA1316】 Supermarket--普通并查集+贪心
  7. [pytorch] PyTorch Hook
  8. ELK(ElasticSearch, Logstash, Kibana) 实现 Java 分布式系统日志分析架构
  9. 用单元测试来调试SilverFish AI
  10. ELMO及前期工作 and Transformer及相关论文