自定义view(2)-android自定义组合控件

在开发中,经常会遇到很多控件用在一起,并且用在很多地方,造成布局文件看起来很乱,当然我们可以用include,merge,viewStub标签来优化标签。本文采用自定义控件的方式来优化

  1. 自定义view的组合控件首先需要继承ViewGroup或者它的子类,如LineatLayout,RelativeLauout,FrameLayout等。

    public class MenuItemLayout extends FrameLayout 
    
  2. 重写构造函数

    public MenuItemLayout(Context context) {
    	this(context,null);
    }
    
    public MenuItemLayout( Context context, AttributeSet attrs) {
    	this(context,attrs,0);
    }
    
    public MenuItemLayout( Context context, AttributeSet attrs, int defStyleAttr) {
    	super(context, attrs, defStyleAttr);
    	init(context,attrs);
    }
    

    一般实现前两个构造函数就可以了,第一个构造函数只能在的意思是只能在代码中添加,第二个的可以在代码和xml中添加,第三个是在view有主题属性的时候使用

  3. 当我们重写完构造函数之后,接下来要做的就是加载我们要的布局,这个布局就是我们要组合起来的控件

    LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mView = inflater.inflate(R.layout.menu_item_layout, this, true);
    

    加载完布局之后,直接找我们需要的控件就可以了

    titleTv = (TextView) findViewById(R.id.menu_item_text);
    
  4. 给控件赋值有两种方式,①是通过xml中指定,我们取值,然后赋值给相应的控件。②在代码中我们写一个public方法,提供给外界调用,用来改变控件的值,这里我我合并这一种

    • 通过xml赋值的话,我们需要写一个控件的属性集合,在value下面新建一个attrs.xml文件,在attrs文件里声明我们需要的属性

      <?xml version="1.0" encoding="utf-8"?>
      <resources>
          <declare-styleable name="MenuItemLayout">
              <attr name="title_text" format="string"/>
              <attr name="hint_text" format="string"/>
              <attr name="icon_reference" format="reference"/>
              <attr name="icon_uri" format="string"/>
              <attr name="jump_url" format="string"/>
              <attr name="divider_line_style" format="integer"/>
          </declare-styleable>
      </resources>
      

      name属性可以随便写(当然要有意义),format是这个attr的数据类型。写完这个之后我们可以在我们的组合控件里获取这些属性的值,然后赋值给相应的控件。

      TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.MenuItemLayout);
      //获取属性的值 a.getString(R.styleable.MenuItemLayout_title_text)
      setTitleText(a.getString(R.styleable.MenuItemLayout_title_text));
      
      //既可以在代码里调用给控件赋值,又可以通过给xml里的属性给控件赋值
      public void setTitleText(String titleText) {
          if (!TextUtils.isEmpty(titleText)){
              this.titleText = titleText;
              titleTv.setText(titleText);
          }
      }
      

      完整代码

      public class MenuItemLayout extends FrameLayout {
      
          private Context mContext;
          private View mView;
          private TextView titleTv, hintTv;
          private ImageView iconImg,redHintImg;
          private String titleText,hintText,jumpUrl,onclickId;
          private int iconImageId;
          public static final int NO_LINE = 0;
          public static final int DIVIDE_LINE = 1;
          public static final int DIVIDE_AREA = 2;
          private int divideLineStyle = NO_LINE;
          private boolean isShowRedHintImg;
      
          public boolean isShowRedHintImg() {
              return isShowRedHintImg;
          }
      
          public void setShowRedHintImg(boolean showRedHintImg) {
              isShowRedHintImg = showRedHintImg;
              redHintImg.setVisibility(showRedHintImg ? VISIBLE : GONE);
          }
      
          public String getJumpUrl() {
              return jumpUrl;
          }
      
          private void setJumpUrl(String jumpUrl) {
              if (!TextUtils.isEmpty(jumpUrl)){
                  this.jumpUrl = jumpUrl;
              }
          }
      
          public int getIconImageId() {
              return iconImageId;
          }
      
          private void setIconImageId(int iconImageId) {
              if (iconImageId != 10000){
                  this.iconImageId = iconImageId;
                  iconImg.setImageResource(iconImageId);
              }
          }
      
          public String getHintText() {
              return hintText;
          }
      
          private void setHintText(String hintText) {
              if (!TextUtils.isEmpty(hintText)){
                  this.hintText = hintText;
                  hintTv.setText(hintText);
              }
          }
      
          public String getTitleText() {
              return titleText;
          }
      
          public void setTitleText(String titleText) {
              if (!TextUtils.isEmpty(titleText)){
                  this.titleText = titleText;
                  titleTv.setText(titleText);
              }
          }
      
          public String getOnclickId() {
              return onclickId;
          }
      
          public void setOnclickId(String onclickId) {
              this.onclickId = onclickId;
          }
      
          public MenuItemLayout(Context context) {
              this(context,null);
          }
      
          public MenuItemLayout( Context context, AttributeSet attrs) {
              this(context,attrs,0);
          }
      
          public MenuItemLayout( Context context, AttributeSet attrs, int defStyleAttr) {
              super(context, attrs, defStyleAttr);
              init(context,attrs);
          }
      
          private void init(Context context, AttributeSet attrs) {
              this.mContext = context;
              LayoutInflater inflater = 	(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              mView = inflater.inflate(R.layout.menu_item_layout, this, true);
              titleTv = (TextView) findViewById(R.id.menu_item_text);
              hintTv = (TextView) findViewById(R.id.menu_item_text_hint);
              iconImg = (ImageView) findViewById(R.id.menu_item_icon_img);
              redHintImg = (ImageView) findViewById(R.id.menu_item_red_hint);
      
              TypedArray a = mContext.obtainStyledAttributes(attrs,R.styleable.MenuItemLayout);
              setTitleText(a.getString(R.styleable.MenuItemLayout_title_text));
              setHintText(a.getString(R.styleable.MenuItemLayout_hint_text));
              setIconImageId(a.getResourceId(R.styleable.MenuItemLayout_icon_reference,10000));
              setJumpUrl(a.getString(R.styleable.MenuItemLayout_jump_url));
              divideLineStyle = a.getInt(R.styleable.MenuItemLayout_divider_line_style, NO_LINE);
              setDivideLine(divideLineStyle);
              a.recycle();
          }
      
          private void setDivideLine(int bottomLineStyle) {
              View lineView = findViewById(R.id.divider_line_view);
              View areaView = findViewById(R.id.divider_area_view);
              lineView.setVisibility(GONE);
              areaView.setVisibility(GONE);
              if (bottomLineStyle == DIVIDE_LINE){
                  lineView.setVisibility(VISIBLE);
              }else if (bottomLineStyle == DIVIDE_AREA){
                  areaView.setVisibility(VISIBLE);
              }
          }
      
          public TextView getTitleTv() {
              return titleTv;
          }
      
          public TextView getHintTv() {
              return hintTv;
          }
      }
      

      布局文件

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/menu_item_layout"
          android:orientation="vertical"
          android:background="@android:color/white"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
      
          <View
              android:id="@+id/divider_line_view"
              android:layout_width="match_parent"
              android:layout_height="@dimen/dp_0_5"
              android:layout_marginLeft="@dimen/dp_50"
              android:background="@color/divider"
              android:visibility="gone"/>
      
          <View
              android:id="@+id/divider_area_view"
              android:layout_width="match_parent"
              android:layout_height="@dimen/dp_10"
              android:background="@color/main_bg"/>
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="@dimen/dp_50"
              android:gravity="center_vertical"
              android:orientation="horizontal"
              android:paddingLeft="@dimen/dp_15"
              android:paddingRight="@dimen/dp_15">
      
              <ImageView
                  android:id="@+id/menu_item_icon_img"
                  android:layout_width="@dimen/dp_24"
                  android:layout_height="@dimen/dp_24" />
      
              <TextView
                  android:id="@+id/menu_item_text"
                  android:layout_width="@dimen/dp_0"
                  android:layout_weight="1"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="@dimen/dp_10"
                  android:gravity="center_vertical"
                  android:textSize="@dimen/sp_15"
                  android:text="标题文字"/>
      
              <TextView
                  android:id="@+id/menu_item_text_hint"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textColor="@color/divider"
                  android:textSize="@dimen/sp_13"
                  android:layout_marginRight="@dimen/dp_10"
                  android:text="提示文字"/>
      
              <ImageView
                  android:id="@+id/menu_item_red_hint"
                  android:layout_width="@dimen/dp_10"
                  android:layout_height="@dimen/dp_10"
                  android:layout_marginRight="@dimen/dp_10"
                  android:visibility="gone"/>
      
              <ImageView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:src="@mipmap/my_return"/>
      
          </LinearLayout>
      
      </LinearLayout>
      

      属性集合

      <?xml version="1.0" encoding="utf-8"?>
      <resources>
          <declare-styleable name="MenuItemLayout">
              <attr name="title_text" format="string"/>
              <attr name="hint_text" format="string"/>
              <attr name="icon_reference" format="reference"/>
              <attr name="icon_uri" format="string"/>
              <attr name="jump_url" format="string"/>
              <attr name="divider_line_style" format="integer"/>
          </declare-styleable>
      </resources>
      
发布了62 篇原创文章 · 获赞 45 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/parade0393/article/details/99711170