构造Android复合控件
2016-03-29 10:44
429 查看
在Android的界面设计中,经常使用到比较类似的布局,比如说:系统设置等等地方,而这些布局每次单独的使用会使用大量的代码进行构建,在这里我给各位介绍一种复合控件的使用,如有不对之处,请指教。1.单个子项的代码:单个子项的布局文件就相当于ListView的item类似,实际上就是界面上相似的布局尽最大的可能统一到一个布局文件中,容纳拥有最多的控件的布局,通过属性值来控制布局中各个控件的显示。可以尽可能多的把单个控件结合进来,但是这同时得增多对界面控制的属性。依据实际情况而定。
<?xml version="1.0" encoding="utf-8"?>2.自定义Android的属性:在values文件加下新建resources.xml文件,在其中写入自定义的属性集(自己取得名字,如果哪位知道真正的名字,请指教),并且设置其中的各个属性的类型(各个属性的类型设置可以参考此博客http://www.cnblogs.com/rayray/p/3442026.html)。这些属性用来控制你的单个子项界面显示效果以及其中各自的属性,根据自己想要实现何种控制,为调用复合控件提供足够的接口来进行子项界面的控制。注意如果多个地方使用统一名字,必须将属性设置至全局中.也就是与declare-styleable同一级。3.自定义复合控件类:这里是将子项界面布局与属性集相结合,是整个复合控件的核心。该类中建议尽量使用多的接口给之后的代码控制子项界面提供入口。
import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.ToggleButton; import com.sunwah.ui.R; public class SystemItemInfo extends LinearLayout { private TextView system_tv_title; private TextView system_tv_value; private ToggleButton system_tb_msg_service; private ImageView system_info_item_img; private LinearLayout system_info_item_layout; private LinearLayout root = null; 4000 /** * 为界面中的控件设置属性方法,利用之后操作界面的显示 * * @return */ public String getTitle() { if (system_tv_title != null) return system_tv_title.getText().toString(); return null; } public void setTitle(String title) { if (title != null) { system_tv_title.setText(title); } } public String getValue() { if (system_tv_value != null) return system_tv_value.getText().toString(); return null; } public void setValue(String value) { if (value != null) { system_tv_value.setText(value); } } public void showValue(boolean flag){ if (system_tv_value != null) { system_tv_value.setVisibility(flag ? View.VISIBLE : View.GONE); } } public void showTb_msg_service(boolean flag) { if (system_tb_msg_service != null) { system_tb_msg_service .setVisibility(flag ? View.VISIBLE : View.GONE); } } public void showLayout(boolean flag) { if (system_info_item_layout != null) { system_info_item_layout.setVisibility(flag ? View.VISIBLE : View.GONE); } } public void showImg(boolean flag) { if (system_info_item_img != null) { system_info_item_img.setVisibility(flag ? View.VISIBLE : View.GONE); } } public ToggleButton getSystem_tb_msg_service() { return system_tb_msg_service; } public LinearLayout getLayout() { return system_info_item_layout; } public SystemItemInfo(Context context) { super(context); } public SystemItemInfo(Context context, AttributeSet attrs) { super(context, attrs); // 用于构建界面文件 init(context, attrs); } /** * 用于构建界面文件 * * @param context * 上下文 * @param attrs * 属性数组 */ private void init(Context context, AttributeSet attrs) { root = (LinearLayout) View.inflate(context, R.layout.layout_system_info_item, this); system_tv_title = (TextView) root.findViewById(R.id.system_tv_title); system_tv_value = (TextView) root.findViewById(R.id.system_tv_value); system_tb_msg_service = (ToggleButton) root .findViewById(R.id.system_tb_msg_service); system_info_item_img = (ImageView) root.findViewById(R.id.system_info_item_img); system_info_item_layout = (LinearLayout) root.findViewById(R.id.system_info_item_layout); TypedArray arr = context.obtainStyledAttributes(attrs,R.styleable.system_info_item); String system_tv_title_info = arr.getString(R.styleable.system_info_item_title); String system_tv_value_info = arr.getString(R.styleable.system_info_item_value); boolean showValue=arr.getBoolean(R.styleable.system_info_item_showValue, false); boolean showTb = arr.getBoolean(R.styleable.system_info_item_showTb,false); boolean showImg = arr.getBoolean(R.styleable.system_info_item_showImg,true); boolean showLayout=arr.getBoolean(R.styleable.system_info_item_showLayout, true); int bgId = arr.getResourceId(R.styleable.system_info_item_background, R.color.all_transparent); if(system_info_item_layout != null){ if(getResources().getDrawable(bgId) != null){ system_info_item_layout.setBackgroundResource(bgId); }else{ system_info_item_layout.setBackgroundColor(getResources().getColor(bgId)); } } if (system_tv_title_info != null) { system_tv_title.setText(system_tv_title_info); } else { system_tv_title.setText(""); } if (system_tv_value_info != null) { system_tv_value.setText(system_tv_value_info); } else { system_tv_value.setText(""); } if(system_tv_value!=null){ system_tv_value.setVisibility(showValue ? View.VISIBLE: View.GONE); } if (system_tb_msg_service != null) { system_tb_msg_service.setVisibility(showTb ? View.VISIBLE: View.GONE); } if (system_info_item_img != null) { system_info_item_img.setVisibility(showImg ? View.VISIBLE: View.GONE); } if (system_info_item_layout != null) { system_info_item_layout.setVisibility(showLayout ? View.VISIBLE: View.GONE); } arr.recycle(); } }4.使用复合控件:在这里我们需要引入自定义的属性集,通过xmlns:infoitem="http://schemas.android.com/apk/res/com.example.test",com.example.test为你项目的包名,前缀是相同的,这样我们就可以像Android中定义的属性一样进行使用,比如说infoitem:title=""便是使用之前我们定义的属性集中的属性,之后进行页面加载的时候,Android机制通过我们自定义的复合控件类进行设置,这样便达到我们的效果。
<?xml version="1.0" encoding="utf-8"?>5.使用复合控件的接口:下面是通过代码进行对复合控件的调用,可以通过代码进行控制界面的显示以及为其中的控件设置各种事件,就如同我们使用EditText等Android自带的控件一样,只不过我们调用的是我们自定义的一些方法和接口。
//简单对自定义复合控件接口的使用 SystemItemInfo btnSystem = (SystemItemInfo) findViewById(R.id.btn_system); btnSystem.showTb_msg_service(true); btnSystem.showImg(false); //对自定义复合控件设置监听 private void setMsgServiceListener() { btn_msg_service.getLayout().setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { btn_msg_service.getSystem_tb_msg_service().toggle(); } }); }
相关文章推荐
- Android GridView属性
- 最强 Android Studio 使用小技巧和快捷键
- Android——Service(服务)
- Android中Action Bar的使用
- android播放提示音工具类(SoundConfigUtils)
- Android控件之CheckBox、RadioGroup、RadioButton
- IntentService: Providing data back to Activity #android
- Android应用出错 error: Error retrieving parent for item: No resource found that matches .....
- 博客第一天上线
- Android 快速开发系列 打造万能的ListView GridView 适配器
- Android 快速开发系列 打造万能的ListView GridView 适配器
- <Android>从窗口泄漏谈android:configChanges属性
- android import library switch语句报错case expressions must be constant expressions
- Android APK 在32bit 和64bit 的区别问题
- 启动Android Virtual Device(android虚拟机)的时发生canot launch AVD in emulator错误
- Android Image压缩工具类
- Android Studio中自己定义快捷输入块
- Android ResultReceiver 汇总
- Android调用百度地图Web端接口,实现百度定位、导航
- 终于调通了从上到下,从驱动到应用,android