android 自定义控件 1
来源:互联网 发布:大连知润信息科技骗局 编辑:程序博客网 时间:2024/06/06 00:54
下面这种自定义控件相当于将多个控件进行集合,进而在管理类中进行相应的属性修改,这种方式感觉用处不大,但是看到这个方式了,首先表明,这种方式用处不大,所以做了实验,来记录一下:
建立一个集合的xml文件,就是你要将多个文件进行集合的:
custom:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:src="@drawable/install_detall_btn" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="确定" android:textColor="#000000" /></LinearLayout>
将这个xml页面进行绑定到一个类中,进行适配:
public class ImageBtn extends LinearLayout { private ImageView imageView; private TextView textView; public ImageBtn(Context context) { super(context); // TODO Auto-generated constructor stub } public ImageBtn(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.custom_button, this); imageView=(ImageView) findViewById(R.id.imageView1); textView=(TextView)findViewById(R.id.textView1); } //activity设置图片资源 public void setImageResource(int resId) { imageView.setImageResource(resId); } //activity调用进行对textView进行赋值 public void setTextViewText(String text) { textView.setText(text); } /** * 这里可以写出很多的set方法来进行改变属性 */}然后在activity中进行相应的调用,这样就可以进行修改相应的类:
<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /><com.xushuangshuang.androidcomplextest.ImageBtn android:id="@+id/but1" android:layout_width="wrap_content" android:layout_height="wrap_content"/><com.xushuangshuang.androidcomplextest.ImageBtn android:id="@+id/but2" android:layout_width="wrap_content" android:layout_height="wrap_content"/></LinearLayout></span>
下面是activity的调用,因为这样的封装,就是将这个封装了一个类进行处理,
ImageBtn linearLayout1; ImageBtn linearLayout2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); linearLayout1 = (ImageBtn) findViewById(R.id.but1); linearLayout2 = (ImageBtn) findViewById(R.id.but2); linearLayout1.setImageResource(R.drawable.ic_launcher); linearLayout2.setTextViewText("benben"); }这样就可以进行更改页面的值了,也许还没有遇到这种方式进行封装的好处,希望各位说一下这种封装的好处
0 0
- Android自定义控件--1
- android 自定义控件 1
- Android自定义控件1
- Android 自定义控件1
- Android自定义控件1
- android 自定义控件1
- android 自定义控件基础知识1
- Android自定义控件1----基本思路
- Android自定义控件1:自定义topBar
- [Android自定义控件] Android自定义控件
- Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- Android 自定义控件 单页翻书控件
- android虚线控件---自定义控件
- No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=i386).
- Android Library Project 的使用小结以及脚本打包事项
- 在qemu中增加pci设备并用linux驱动验证
- gradle工程转化eclipse工程
- Lua中的userdata
- android 自定义控件 1
- Vm centos 设置静态IP地址
- 【数据结构】二叉树
- 侧边固定悬浮导航
- 【Spring】官网教程阅读笔记(五):Redis消息
- **没有规则可以创建“XXX”需要的目标“XXX”问题的解决方案
- cocos2dx3.2开发 RPG《Flighting》
- 第一天:求二维数组中每个一维数组的元素之和
- excel 解析 xls 和 xlsx