TextView组合控件封装

TextView组合控件封装_第1张图片
Paste_Image.png

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

一、先上布局

  
  






二、自定义属性

   
  
     
     
  

三、自定义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();
}
}

四、使用

  

就会实现如下效果

Paste_Image.png

你可能感兴趣的:(TextView组合控件封装)