本篇文章为大家展示了Android中怎么利用组合控件复用布局,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
首先,我们需要写出布局文件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>
定义自定义属性
<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>
自定义一个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;}}
正确地使用它
<?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>
上述内容就是Android中怎么利用组合控件复用布局,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注编程网行业资讯频道。