android组合控件 重叠,Android 组合控件实现布局的复用的方法
看到很多項目會有實現自己的標題欄的做法,通常的界面是左邊按鈕或文字,加上中間的標題和右邊的按鈕或文字組成的。比較好的一種做法是使用include標簽,復用同一個xml文件來實現布局的復用。但是這種方法是通過代碼的方式來設置標題,左右按鈕等其他的屬性,會導致布局屬性和Activity代碼耦合性比較高。
因此,我們要通過自定義View,繼承ViewGroup子類來實現這樣的布局,降低布局文件和Activity代碼耦合性。
首先,我們需要寫出布局文件layout_custom_titlebar.xml。
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" />
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" />
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" />
2.定義自定義屬性
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.正確地使用它
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
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" />
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="右邊"/>
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="右邊"/>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。
總結
以上是生活随笔為你收集整理的android组合控件 重叠,Android 组合控件实现布局的复用的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在html中怎么写加起来的,css可以在
- 下一篇: android radiogroup 获