【Android-布局复用】 多个界面复用一个布局文件(一)
1.layout_common.xml
复用的布局文件
<?xml version="1.0" encoding="utf-8"?>
<!-- 复用的布局文件 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" /> <Button
android:id="@+id/common_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" /> <Button
android:id="@+id/common_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" /> <Button
android:id="@+id/common_button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" /> </LinearLayout>
2.layout_main.xml
主布局文件 ,在这里引用复用的布局文件
<?xml version="1.0" encoding="utf-8"?>
<!-- 主布局文件 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="16dp"
android:layout_weight="1" >
</RelativeLayout> <!-- 在布局文件中引用复用的布局文件 --> <include layout="@layout/layout_common" /> </LinearLayout>
3.CommonView.java
复用布局文件实例化。单独封装,接口回调。 避免重复写布局文件,避免重复实例化控件,避免重复设置监听方法
package com.example.mytestapp; import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; /**
* 复用布局文件实例化
*/
public class CommonView implements OnClickListener { // 接口
public interface OnCommonViewClick {
public void onButton1Click(View v); public void onButton2Click(View v); public void onButton3Click(View v);
} public void setListener(OnCommonViewClick listener) {
this.listener = listener;
} Context mContext;
OnCommonViewClick listener; public CommonView(Context context) {
this.mContext = context;
} public Button button1, button2, button3; public CommonView init() {
button1 = (Button) ((Activity) mContext).findViewById(R.id.common_button1);
button2 = (Button) ((Activity) mContext).findViewById(R.id.common_button2);
button3 = (Button) ((Activity) mContext).findViewById(R.id.common_button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
return this;
} @Override
public void onClick(View v) {
if (listener == null)
return;
switch (v.getId()) {
case R.id.common_button1:
listener.onButton1Click(v);
break;
case R.id.common_button2:
listener.onButton2Click(v);
break;
case R.id.common_button3:
listener.onButton3Click(v);
break;
default:
break;
}
} }
4.MainActivity.java
主界面实例化
package com.example.mytestapp; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast; /*
* 问题:
* 1.如何用代码改变控件的文字或颜色?
*/ public class MainActivity extends Activity implements CommonView.OnCommonViewClick { int clickTimes = 0; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
// 在这里实例化布局文件, 并实现监听接口方法
// 只需要一行代码就可以直接完成复用代码块的实例化
new CommonView(this).init().setListener(this);
} @Override
public void onButton1Click(View v) {
clickTimes++;
// 在这里实现对应点击事件的方法
Toast.makeText(this, "你点击了button" + clickTimes + "次", Toast.LENGTH_SHORT).show(); } @Override
public void onButton2Click(View v) {
// 在这里实现对应点击事件的方法
} @Override
public void onButton3Click(View v) {
// 在这里实现对应点击事件的方法
} }
【Android-布局复用】 多个界面复用一个布局文件(一)的更多相关文章
- Android 手机卫士--实现设置界面的一个条目布局结构
本文地址:http://www.cnblogs.com/wuyudong/p/5908986.html,转载请注明源地址. 本文以及后续文章,将一步步完善功能列表: 要点击九宫格中的条目,需要注册点击 ...
- 【Android-布局复用】 多个界面复用一个布局文件(二)
多个界面复用一个布局界面 ,如何找到复用布局文件中的控件的id? 举个栗子: 1. layout_common.xml 复用的布局文件,如何找到button 的id? <?xml versio ...
- Android性能优化之中的一个 布局优化
本文为Android性能优化--布局优化,主要介绍使用抽象布局标签(include, viewstub, merge).去除不必要的嵌套和View节点.降低不必要的infalte及其它Layout方面 ...
- android界面设计之布局管理
谈到android界面设计,各种布局样式不得不提!传统的布局方式有6种,我们会一一介绍. 在android studio2.2版本之后出现了一款超棒的布局方式,真正意义上的所见即所得,后面我们也会讲到 ...
- freemarker实现通用布局的模板拆分与复用
原文:http://www.hawu.me/coding/733 一.基础页面布局 假设我们项目页面的通用布局如下图所示: 实现这样的布局的基本html代码如下: XHTML ...
- Android 手机卫士--导航界面1的布局编写
本文地址:http://www.cnblogs.com/wuyudong/p/5943005.html,转载请注明出处. 本文实现导航界面1的布局的实现,效果如下图所示: 首先分析所使用的布局样式: ...
- android 引入一个布局库后该有的操作
背景 引入一个布局库:com.zhy:percent-support-extends 然后sync now 成功了,也就是同步成功了. 然而开始使用的时候报告了: The following clas ...
- Android笔记——在布局文件中插入另一个布局文件
假如有一个布局文件A.xml想把另外一个布局文件B.xml引进其布局,则可以通过下面的代码 <include layout="@layout/B" />
- Android Studido下的应用性能优化总结--布局优化
前言:一个应用的成功=产品设计*性能 ,再此我们不讨论一个应用的设计,那交给我们可爱又可恨的产品经理和UI设计师来决定!所以这里分步骤讨论如何提升一个应用的性能,这里先探讨布局优化问题. 布局优化 避 ...
随机推荐
- 编译+远程调试spark
一 编译 以spark2.4 hadoop2.8.4为例 1,spark 项目根pom文件修改 pom文件新增 <profile> <id>hadoop-2.8</id ...
- k8s集群升级
集群升级 由于课程中的集群版本是 v1.10.0,这个版本相对有点旧了,最新版本都已经 v1.14.x 了,为了尽量保证课程内容的更新度,所以我们需要将集群版本更新.我们的集群是使用的 kubeadm ...
- python+django学习二
所有模型类型的准备和迁移 在setting.py中添加:AUTH_USER_MODEL = 'users.UserProfile' 继承用户模板 确保子项目的url现在都是空的, 在pycharm的f ...
- SP338ROADS题解--最短路变式
题目链接 https://www.luogu.org/problemnew/show/SP338 分析 联想到不久前做过的一道题\(Full\) \(Tank\),感觉可以用优先队列做,于是写了\(d ...
- SQL将多行数据合并成一行【转】
转:https://blog.csdn.net/AntherFantacy/article/details/83824182 今天同事问了一个需求,就是将多行数据合并成一行进行显示,查询了一些资料,照 ...
- Python网络编程常用代码
服务器端代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 # -*- coding: cp936 -*- ...
- 05_Hive分区总结
2.1.创建分区表并将本地文件的数据加载到分区表: 使用下面的命令来创建一个带分区的表 通过partitioned by(country string)关键字声明该表是分区表,且分区字段不能为crea ...
- apache安装phpMyAdmin
安装phpMyAdmin 我这里是LAMP环境 安装httpd,和phpMyAdmin,数据库可以yum安装看你自己情况选择安装方式 $ yum -y install httpd phpMyAdmin ...
- JS中数组初始化以及赋值
.指定长度,然后初始化 ); ;index < ;index++){ vArray[index] = index; } 2.不指定长度,然后初始化 var vArray = new Array( ...
- nginx+tomcat实现负载均衡以及双机热备
还记得那些年吗? 还记得更新代码之后,服务器起不来被领导训斥吗?还记得更新代码,需要停机过多的时间被渠道部们埋怨吗?还记得更新代码,代码出错时自己吓个半死吗?于是我们聪明勤快的程序员,看着电影待到夜深 ...