自定义组合控件
来源:互联网 发布:java定义一个二维数组 编辑:程序博客网 时间:2024/05/22 14:35
1、我们常见的效果如下:
如果不封装,每一次层的代码都是两个Textview,那么我们可以创建一个单独的View来简化代码。
自定义组合View的代码如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:background="@color/white" android:layout_height="45dp"> <TextView android:id="@+id/m_tv_ordername" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="15dp" android:text="订单编号" android:textColor="@color/text_bigtitle" android:textSize="14sp" /> <TextView android:id="@+id/m_tv_ordervalue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" android:ellipsize="end" android:maxLength="35" android:singleLine="true" android:text="1111" android:textColor="@color/text_bigtitle" android:textSize="14sp" /></RelativeLayout>
效果如下:
2、开始编写自定义组合View,代码如下
分析:由于左侧的值是固定的,我们可以在布局中,直接写,方便浏览。
右侧的值是从服务器获取的,所以我们像textview.settext();一样去设置值。
public class OrderInfoView extends RelativeLayout { TextView m_tv_ordername; TextView m_tv_ordervalue; public OrderInfoView(Context context) { super(context); initView(context); } public OrderInfoView(Context context, AttributeSet attrs) { super(context, attrs); initView(context); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.OrderInfoView); m_tv_ordername.setText(ta.getString(R.styleable.OrderInfoView_leftname)); } public OrderInfoView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context); } private void initView(Context context) { // TODO Auto-generated method stub View.inflate(context, R.layout.m_orderinfo, this); m_tv_ordername = (TextView) findViewById(R.id.m_tv_ordername); m_tv_ordervalue = (TextView) findViewById(R.id.m_tv_ordervalue); } //设置内容 public void setMTVtext(String mvalue) { m_tv_ordervalue.setText(mvalue); }}
在value文件夹下,创建attrs.xml,定义自定义属性
代码如下:
<?xml version="1.0" encoding="utf-8"?><resources> <!-- 订单详情自定义view--> <declare-styleable name="OrderInfoView"> <attr name="leftname" format="string" /> <!-- format : 类型 --> </declare-styleable></resources>
3、接下来就可以直接在我们的布局文件中调用了
注意,要在父布局加入命名空间的引入。
1 0
- 自定义控件-组合控件
- 自定义组合控件,组合模式
- Android自定义组合控件
- android自定义组合控件
- 自定义组合控件
- 1.11 自定义组合控件
- 自定义组合控件
- 自定义组合控件
- 自定义组合控件
- 自定义组合控件.txt
- Android自定义组合控件
- 07自定义组合控件
- 自定义组合控件
- android组合自定义控件
- android 自定义组合控件
- 自定义组合控件
- android 自定义组合控件
- android 自定义组合控件
- (转载)【机器学习算法系列之二】浅析Logistic Regression
- Android 6.0 scrollview 嵌套recyclerview不能全部显示,高度不正常的问题
- LeetCode专题----Tree
- HttpClient和HttpUrlConnection实现post发送和接收json数据
- leetcode 442 Find All Duplicates in an Array C++
- 自定义组合控件
- MySQL数据库 1067号错误的解决办法
- 侧拉删除
- ModBUS的一些好文章
- python学习笔记,1安装Python
- tcp为什么要三次握手,而不能二次握手?
- 使用tcpdump、wireshark对Android应用程序进行抓包并分析
- Linux内核驱动开发之KGDB原理介绍及kgdboe方式配置
- 第一章 导言