当前位置: 移动技术网 > 移动技术>移动开发>Android > Android 组合控件实现布局的复用的方法

Android 组合控件实现布局的复用的方法

2019年07月24日  | 移动技术网移动技术  | 我要评论

看到很多项目会有实现自己的标题栏的做法,通常的界面是左边按钮或文字,加上中间的标题和右边的按钮或文字组成的。比较好的一种做法是使用include标签,复用同一个xml文件来实现布局的复用。但是这种方法是通过代码的方式来设置标题,左右按钮等其他的属性,会导致布局属性和activity代码耦合性比较高。

因此,我们要通过自定义view,继承viewgroup子类来实现这样的布局,降低布局文件和activity代码耦合性。

首先,我们需要写出布局文件layout_custom_titlebar.xml。

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 使用merge标签减少层级 -->
<button
  android:id="@+id/title_bar_left"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignparentleft="true"
  android:layout_centervertical="true"
  android:layout_marginleft="5dp"
  android:background="@null"
  android:minheight="45dp"
  android:minwidth="45dp"
  android:textsize="14sp" />

<textview
  android:id="@+id/title_bar_title"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerinparent="true"
  android:singleline="true"
  android:textsize="17sp" />

<button
  android:id="@+id/title_bar_right"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignparentright="true"
  android:layout_centervertical="true"
  android:layout_marginright="7dp"
  android:background="@null"
  android:minheight="45dp"
  android:minwidth="45dp"
  android:textsize="14sp" />

</merge>

2.定义自定义属性

<declare-styleable name="customtitlebar">
  <!--标题栏背景色-->
  <attr name="title_background_color" format="reference|integer" />
  <!--左边按钮是否可见-->
  <attr name="left_button_visible" format="boolean" />
  <!--右边按钮是否可见-->
  <attr name="right_button_visible" format="boolean" />
  <!--标题文字-->
  <attr name="title_text" format="string" />
  <!--标题文字颜色-->
  <attr name="title_text_color" format="color" />
  <!--标题文字图标-->
  <attr name="title_text_drawable" format="reference|integer" />
  <!--左边按钮文字-->
  <attr name="left_button_text" format="string" />
  <!--左边按钮文字颜色-->
  <attr name="left_button_text_color" format="color" />
  <!--左边按钮图标-->
  <attr name="left_button_drawable" format="reference|integer" />
  <!--右边按钮文字-->
  <attr name="right_button_text" format="string" />
  <!--右边按钮文字颜色-->
  <attr name="right_button_text_color" format="color" />
  <!--右边按钮图标-->
  <attr name="right_button_drawable" format="reference|integer" />
</declare-styleable>

3.自定义一个view继承viewgroup子类,这里我们继承relativelayout。

public class customtitlebar extends relativelayout {
private button titlebarleftbtn;
private button titlebarrightbtn;
private textview titlebartitle;

public customtitlebar(context context) {
  super(context);
}

public customtitlebar(context context, attributeset attrs) {
  super(context, attrs);

  layoutinflater.from(context).inflate(r.layout.layout_custom_titlebar,this,true);
  titlebarleftbtn = (button) findviewbyid(r.id.title_bar_left);
  titlebarrightbtn = (button) findviewbyid(r.id.title_bar_right);
  titlebartitle = (textview) findviewbyid(r.id.title_bar_title);

  typedarray typedarray=context.obtainstyledattributes(attrs,r.styleable.customtitlebar);
  if(typedarray!=null){
    //titlebar背景色
    int titlebarbackground=typedarray.getresourceid(r.styleable.customtitlebar_title_background_color, color.blue);
    setbackgroundcolor(titlebarbackground);

    //获取是否要显示左边按钮
    boolean leftbuttonvisible = typedarray.getboolean(r.styleable.customtitlebar_left_button_visible, true);
    if (leftbuttonvisible) {
      titlebarleftbtn.setvisibility(view.visible);
    } else {
      titlebarleftbtn.setvisibility(view.invisible);
    }
    //设置左边按钮的文字
    string leftbuttontext = typedarray.getstring(r.styleable.customtitlebar_left_button_text);
    if (!textutils.isempty(leftbuttontext)) {
      titlebarleftbtn.settext(leftbuttontext);
      //设置左边按钮文字颜色
      int leftbuttontextcolor = typedarray.getcolor(r.styleable.customtitlebar_left_button_text_color, color.white);
      titlebarleftbtn.settextcolor(leftbuttontextcolor);
    } else {
      //设置左边图片icon 这里是二选一 要么只能是文字 要么只能是图片
      int leftbuttondrawable = typedarray.getresourceid(r.styleable.customtitlebar_left_button_drawable, r.mipmap.titlebar_back_icon);
      if (leftbuttondrawable != -1) {
        titlebarleftbtn.setbackgroundresource(leftbuttondrawable);
      }
    }

    //先获取标题是否要显示图片icon
    int titletextdrawable = typedarray.getresourceid(r.styleable.customtitlebar_title_text_drawable, -1);
    if (titletextdrawable != -1) {
      titlebartitle.setbackgroundresource(titletextdrawable);
    } else {
      //如果不是图片标题 则获取文字标题
      string titletext = typedarray.getstring(r.styleable.customtitlebar_title_text);
      if (!textutils.isempty(titletext)) {
        titlebartitle.settext(titletext);
      }
      //获取标题显示颜色
      int titletextcolor = typedarray.getcolor(r.styleable.customtitlebar_title_text_color, color.white);
      titlebartitle.settextcolor(titletextcolor);
    }

    //获取是否要显示右边按钮
    boolean rightbuttonvisible = typedarray.getboolean(r.styleable.customtitlebar_right_button_visible, true);
    if (rightbuttonvisible) {
      titlebarrightbtn.setvisibility(view.visible);
    } else {
      titlebarrightbtn.setvisibility(view.invisible);
    }
    //设置右边按钮的文字
    string rightbuttontext = typedarray.getstring(r.styleable.customtitlebar_right_button_text);
    if (!textutils.isempty(rightbuttontext)) {
      titlebarrightbtn.settext(rightbuttontext);
      //设置右边按钮文字颜色
      int rightbuttontextcolor = typedarray.getcolor(r.styleable.customtitlebar_right_button_text_color, color.blue);
      titlebarrightbtn.settextcolor(rightbuttontextcolor);
    } else {
      //设置右边图片icon 这里是二选一 要么只能是文字 要么只能是图片
      int rightbuttondrawable = typedarray.getresourceid(r.styleable.customtitlebar_right_button_drawable, -1);
      if (rightbuttondrawable != -1) {
        titlebarrightbtn.setbackgroundresource(rightbuttondrawable);
      }
    }
    typedarray.recycle();
  }

}

public void settitleclicklistener(onclicklistener onclicklistener) {
  if (onclicklistener != null) {
    titlebarleftbtn.setonclicklistener(onclicklistener);
    titlebarrightbtn.setonclicklistener(onclicklistener);
  }
}

public button gettitlebarleftbtn() {
  return titlebarleftbtn;
}

public button gettitlebarrightbtn() {
  return titlebarrightbtn;
}

public textview gettitlebartitle() {
  return titlebartitle;
}
}

4.正确地使用它

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.mumubin.demoproject.view.customtitlebar
  android:id="@+id/ctb_view"
  android:layout_width="match_parent"
  android:layout_height="45dp"
  app:right_button_drawable="@mipmap/sure"
  app:title_text="@string/app_name" />

<com.mumubin.demoproject.view.customtitlebar
  android:layout_width="match_parent"
  android:layout_height="45dp"
  android:layout_margintop="4dp"
  app:title_background_color="@color/colorprimary"
  app:title_text="@string/app_name"
  app:title_text_color="@color/coloraccent"
  app:left_button_text="左边"
  app:right_button_text="右边"/>

<com.mumubin.demoproject.view.customtitlebar
  android:layout_width="match_parent"
  android:layout_height="45dp"
  android:layout_margintop="4dp"
  app:title_text_drawable="@mipmap/ic_launcher"
  app:title_background_color="@color/coloraccent"
  app:left_button_text="左边"
  app:right_button_text="右边"/>
</linearlayout>


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网