TextView组合控件封装

Paste_Image.png

每个应用都有个人中心,可能其他页面也有类似,如果每个页面都去写比较麻烦,我们可以封装为一个控件使用

一、先上布局

  <?xml version="1.0" encoding="utf-8"?>
  <com.zhy.autolayout.AutoRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="match_parent"
  android:background="@color/white"
  android:layout_height="90px">

<TextView
    android:id="@+id/m_tv_infoname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="30px"
    android:text="订单编号"
    android:textColor="@color/login_line_focus"
    android:textSize="28px" />

<TextView
    android:id="@+id/m_tv_infovalue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/iv_you"
    android:layout_marginRight="20px"
    android:ellipsize="end"
    android:maxLength="35"
    android:singleLine="true"
    android:textColor="@color/login_line_focus"
    android:textSize="28px" />
<ImageView
    android:id="@+id/iv_you"
    android:src="@mipmap/you"
    android:layout_marginRight="30px"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</com.zhy.autolayout.AutoRelativeLayout>

二、自定义属性

   <!-- 自定义view-->
  <declare-styleable name="InfoTextView">
    <attr name="leftname" format="string" /> <!-- format : 类型 -->
    <attr name="righttext" format="string" /> <!-- format : 类型 -->
  </declare-styleable>

三、自定义view

    public class InfoTextView extends RelativeLayout {
    TextView  m_tv_ordername;
   TextView m_tv_ordervalue;
public InfoTextView(Context context) {
    super(context);
    initView(context);
}
public InfoTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initView(context);
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.InfoTextView);
    m_tv_ordername.setText(ta.getString(R.styleable.InfoTextView_leftname));
    m_tv_ordervalue.setText(ta.getString(R.styleable.InfoTextView_righttext));

}
public InfoTextView(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_infotext, this);
    m_tv_ordername = (TextView) findViewById(R.id.m_tv_infoname);
    m_tv_ordervalue = (TextView) findViewById(R.id.m_tv_infovalue);
}
//设置内容
public void setMTVtext(String mvalue)
{
    m_tv_ordervalue.setText(mvalue);
}
public String getMTVtext()
{
    return m_tv_ordervalue.getText().toString().trim();
}
}

四、使用

  <com.yaolai.client.view.InfoTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:leftname="姓名"
    app:righttext="张三"/>

就会实现如下效果

Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容