自定义控件解决重复编码问题
来源:互联网 发布:head first sql pdf 编辑:程序博客网 时间:2024/06/05 07:44
使用自定义控件的方式解决了两个问题:
1、在每一个活动中 重复编写布局代码的问题,
2、在每一个活动中 都需要重新注册一遍返回按钮的点击事件
下面直接看例子:
效果图:
my_layout.xml中的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FF0000" > <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="#32CD99" android:text="返回" 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="标题" android:textColor="#fff" android:textSize="24sp" /> <Button android:id="@+id/title_edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dip" android:background="#6F4242" android:text="编辑" android:textColor="#fff" /></LinearLayout>
myLayout.java中的代码:
package com.demo.mydefinitionlayout;import android.app.Activity;import android.content.Context;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.widget.Button;import android.widget.LinearLayout;import android.widget.Toast;/** * @使用自定义控件的方式解决了两个问题: * @1、在每一个活动中 重复编写布局代码的问题, * @2、在每一个活动中 都需要重新注册一遍返回按钮的点击事件 */public class myLayout extends LinearLayout {public myLayout(Context context, AttributeSet attrs) {super(context, attrs);/** * 对标题栏布局进行动态加载 */LayoutInflater.from(context).inflate(R.layout.my_layout, this);/** * 获得按钮,添加点击事件 */Button title_back = (Button) findViewById(R.id.title_back);Button title_edit = (Button) findViewById(R.id.title_edit);title_back.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub// 销毁当前活动Toast.makeText(getContext(), "您点击了返回按钮", 1).show();((Activity) getContext()).finish();}});title_edit.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubToast.makeText(getContext(), "您点击了编辑按钮", 1).show();}});}}
activity_main.xml中的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!-- 1、添加普通控件 --> <!-- <include layout="@layout/title" /> --> <!-- 2、添加自定义控件,添加自定义控件的时候,需要指明控件的完整类名,包名在这里是不可以省略的 --> <com.demo.mydefinitionlayout.myLayout android:layout_width="match_parent" android:layout_height="wrap_content" > </com.demo.mydefinitionlayout.myLayout></LinearLayout>
MainActivity.java中的代码:
package com.demo.mydefinitionlayout;import android.app.Activity;import android.app.ActionBar;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.os.Build;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉activity自带的标题栏setContentView(R.layout.activity_main);}}
项目下载:
自定义控件项目demo下载地址
0 0
- 自定义控件解决重复编码问题
- android 自定义控件字体,解决字体偏移,卡顿,代码重复等问题
- android 自定义控件字体,解决字体偏移,卡顿,代码重复等问题
- 解决自定义控件大小问题
- 解决自定义控件闪烁的问题
- C# 自定义文本编辑器控件中 中文重复出现问题
- 自定义Toast及解决Toast的重复显示问题
- 解决Asp.net中翻页问题的自定义用户控件
- 解决Asp.net中翻页问题的自定义用户控件
- WinForm解决UserControl自定义控件闪烁的问题
- 解决ScrollView 与 自定义控件中的onTouchEvent 冲突问题
- 解决头部调用用户自定义控件出现乱码问题
- isInEditMode解决可视化编辑器无法识别自定义控件的问题
- Android使用自定义控件遇到的问题及解决
- 自定义控件解决android中TextView中英文换行问题
- 解决可视化编辑器无法识别自定义控件的问题
- Android自定义控件布局刷新自定义控件回到初始位置问题的解决
- ListView 控件下拉重复问题
- 史上最全的 Java 新手问题汇总
- 制作可用grub引导Linux系统的磁盘映像文件 二
- Undefined symbols for architecture i386: _OBJC_CLASS_$_SKPSMTPMessage", referenced from: error
- epoll + 多线程实现并发网络连接处理
- android典型代码系列(十九)------将一个数组复制成为另外一个数组的方法
- 自定义控件解决重复编码问题
- Xcode7.1请求网络数据
- ExtJS 4无限制滚动条的Grid
- OC中的面向对象编程思想<一>
- 大白书 2.4节 组合游戏(博弈论)
- viewpager 滑动 卡死 死机
- 移动端网络基础--HTTP
- 爬取新浪搜索内容遇到的问题及解决
- eclipse项目找不到build path