版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android中怎么利用組合控件復(fù)用布局
本篇文章為大家展示了Android中怎么利用組合控件復(fù)用布局,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。首先,我們需要寫(xiě)出布局文件layout_custom_titlebar.xml。<?xml
version="1.0"
encoding="utf-8"?>
<merge
xmlns:android="/apk/res/android">
<!--
使用merge標(biāo)簽減少層級(jí)
-->
<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">
<!--標(biāo)題欄背景色-->
<attr
name="title_background_color"
format="reference|integer"
/>
<!--左邊按鈕是否可見(jiàn)-->
<attr
name="left_button_visible"
format="boolean"
/>
<!--右邊按鈕是否可見(jiàn)-->
<attr
name="right_button_visible"
format="boolean"
/>
<!--標(biāo)題文字-->
<attr
name="title_text"
format="string"
/>
<!--標(biāo)題文字顏色-->
<attr
name="title_text_color"
format="color"
/>
<!--標(biāo)題文字圖標(biāo)-->
<attr
name="title_text_drawable"
format="reference|integer"
/>
<!--左邊按鈕文字-->
<attr
name="left_button_text"
format="string"
/>
<!--左邊按鈕文字顏色-->
<attr
name="left_button_text_color"
format="color"
/>
<!--左邊按鈕圖標(biāo)-->
<attr
name="left_button_drawable"
format="reference|integer"
/>
<!--右邊按鈕文字-->
<attr
name="right_button_text"
format="string"
/>
<!--右邊按鈕文字顏色-->
<attr
name="right_button_text_color"
format="color"
/>
<!--右邊按鈕圖標(biāo)-->
<attr
name="right_button_drawable"
format="reference|integer"
/>
</declare-styleable>3.自定義一個(gè)View繼承ViewGroup子類(lèi),這里我們繼承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);
}
//設(shè)置左邊按鈕的文字
String
leftButtonText
=
typedArray.getString(R.styleable.CustomTitleBar_left_button_text);
if
(!TextUtils.isEmpty(leftButtonText))
{
titleBarLeftBtn.setText(leftButtonText);
//設(shè)置左邊按鈕文字顏色
int
leftButtonTextColor
=
typedArray.getColor(R.styleable.CustomTitleBar_left_button_text_color,
Color.WHITE);
titleBarLeftBtn.setTextColor(leftButtonTextColor);
}
else
{
//設(shè)置左邊圖片icon
這里是二選一
要么只能是文字
要么只能是圖片
int
leftButtonDrawable
=
typedArray.getResourceId(R.styleable.CustomTitleBar_left_button_drawable,
R.mipmap.titlebar_back_icon);
if
(leftButtonDrawable
!=
-1)
{
titleBarLeftBtn.setBackgroundResource(leftButtonDrawable);
}
}
//先獲取標(biāo)題是否要顯示圖片icon
int
titleTextDrawable
=
typedArray.getResourceId(R.styleable.CustomTitleBar_title_text_drawable,
-1);
if
(titleTextDrawable
!=
-1)
{
titleBarTitle.setBackgroundResource(titleTextDrawable);
}
else
{
//如果不是圖片標(biāo)題
則獲取文字標(biāo)題
String
titleText
=
typedArray.getString(R.styleable.CustomTitleBar_title_text);
if
(!TextUtils.isEmpty(titleText))
{
titleBarTitle.setText(titleText);
}
//獲取標(biāo)題顯示顏色
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);
}
//設(shè)置右邊按鈕的文字
String
rightButtonText
=
typedArray.getString(R.styleable.CustomTitleBar_right_button_text);
if
(!TextUtils.isEmpty(rightButtonText))
{
titleBarRightBtn.setText(rightButtonText);
//設(shè)置右邊按鈕文字顏色
int
rightButtonTextColor
=
typedArray.getColor(R.styleable.CustomTitleBar_right_button_text_color,
Color.BLUE);
titleBarRightBtn.setTextColor(rightButtonTextColor);
}
else
{
//設(shè)置右邊圖片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="/apk/res/android"
xmlns:app="/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_dr
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 《音階歌(唱游、發(fā)現(xiàn))》教學(xué)設(shè)計(jì)-2025-2026學(xué)年接力版(新教材)小學(xué)音樂(lè)一年級(jí)下冊(cè)
- 甲殼類(lèi)養(yǎng)殖工安全實(shí)操測(cè)試考核試卷含答案
- 氣體凈化工崗前理論技能考核試卷含答案
- 碾泥工安全檢查測(cè)試考核試卷含答案
- 我國(guó)上市公司治理結(jié)構(gòu)的理性構(gòu)建之路
- 我國(guó)上市公司并購(gòu)重組盈利補(bǔ)償機(jī)制:實(shí)踐、問(wèn)題與優(yōu)化路徑
- 丁苯橡膠裝置操作工安全應(yīng)急知識(shí)考核試卷含答案
- 苗木培育工崗前安全理論考核試卷含答案
- 鎢絞絲加熱子制造工安全綜合考核試卷含答案
- 水工混凝土維修工達(dá)標(biāo)能力考核試卷含答案
- 淮安市2023-2024學(xué)年七年級(jí)上學(xué)期期末歷史試卷(含答案解析)
- 完整工資表模板(帶公式)
- 家長(zhǎng)要求學(xué)校換老師的申請(qǐng)書(shū)
- 奇瑞汽車(chē)QC小組成果匯報(bào)材料
- 闌尾腫瘤-課件
- CTT2000LM用戶(hù)手冊(cè)(維護(hù)分冊(cè))
- 川2020J146-TJ 建筑用輕質(zhì)隔墻條板構(gòu)造圖集
- 正式員工派遣單
- 新員工入職申請(qǐng)表模板
- 中外新聞事業(yè)史課程教學(xué)大綱
- LY/T 1357-2008歧化松香
評(píng)論
0/150
提交評(píng)論