Android自定義通用標(biāo)題欄CustomTitleBar
本文實(shí)例為大家分享了Android自定義通用標(biāo)題欄的具體代碼,供大家參考,具體內(nèi)容如下/p>
1自定義一個(gè)public_titlebar.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rootView" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/ivLeft" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/z"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <TextView android:id="@+id/tvTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="tvTitle"/> <TextView android:id="@+id/tvRight" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="tvRight"/> </LinearLayout> </LinearLayout>
2.在values文件夾下新建一個(gè)attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CustomerTitleBar"> <attr name="left_image" format="reference"></attr> <attr name="center_text" format="string"></attr> <attr name="center_text_color" format="color"></attr> <attr name="center_text_size" format="dimension"></attr> </declare-styleable> </resources>
3.自定義CustomerTitleBar類(lèi)繼承LinearLayout,統(tǒng)一頁(yè)面標(biāo)題欄,項(xiàng)目中包括接口回調(diào)
public class CustomerTitleBar extends LinearLayout {
private LinearLayout rootView;
private ImageView ivLeft;
private TextView tvTitle;
private TextView tvRight;
//3.聲明回調(diào)對(duì)象
private CustomerClick leftClick;
/**
* @param context
*/
//在代碼中直接new一個(gè)Custom View實(shí)例的時(shí)候,會(huì)調(diào)用第一個(gè)構(gòu)造函數(shù)
public CustomerTitleBar(Context context) {
this(context,null);
}
//在xml布局文件中調(diào)用Custom View的時(shí)候,會(huì)調(diào)用第二個(gè)構(gòu)造函數(shù)
public CustomerTitleBar(Context context,AttributeSet attrs) {
this(context, attrs,-1);
}
//在xml布局文件中調(diào)用Custom View,并且Custom View標(biāo)簽中還有自定義屬性時(shí),這里調(diào)用的還是第二個(gè)構(gòu)造函數(shù).
public CustomerTitleBar(Context context,AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
initAttrs(context,attrs);
initLister();
}
private void init(Context context) {
rootView= (LinearLayout) View.inflate(context,R.layout.layout_customer_title_bar,this);
ivLeft=rootView.findViewById(R.id.ivLeft);
tvTitle=rootView.findViewById(R.id.tvTitle);
tvRight=rootView.findViewById(R.id.tvRight);
}
private void initAttrs(Context context, AttributeSet attrs) {
TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.CustomerTitleBar);
Drawable drawable=typedArray.getDrawable(R.styleable.CustomerTitleBar_left_image);
if (drawable!=null){
ivLeft.setImageDrawable(drawable);
}
CharSequence text = typedArray.getText(R.styleable.CustomerTitleBar_center_text);
if (!TextUtils.isEmpty(text)) {
tvTitle.setText(text);
}
int color = typedArray.getColor(R.styleable.CustomerTitleBar_center_text_color, -1);
if (color != -1) {
tvTitle.setTextColor(color);
}
float dimension = typedArray.getDimension(R.styleable.CustomerTitleBar_center_text_size, 0f);
tvTitle.setTextSize(dimension);
}
private void initLister() {
ivLeft.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//5.使用接口回調(diào)
if (leftClick!=null){
leftClick.onLefClick(v);
}
}
});
}
//4、提供回調(diào)對(duì)象的set方法
public void setLeftClick(CustomerClick leftClick) {
this.leftClick = leftClick;
}
//1.定義回調(diào)接口
interface CustomerClick{
void onLefClick(View view);
}
}
4.在布局文件中的引用
<com.cn.jyx.customertitlebar.CustomerTitleBar android:id="@+id/ctTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" customer:center_text="吐泡泡" customer:center_text_color="#ff00ff" customer:center_text_size="20sp" />
5.在Activity中的用法
public class MainActivity extends AppCompatActivity {
private CustomerTitleBar ctTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ctTitle=findViewById(R.id.ctTitle);
//6、使用
ctTitle.setLeftClick(new CustomerTitleBar.CustomerClick() {
@Override
public void onLefClick(View view) {
Toast.makeText(MainActivity.this,"吐泡泡",Toast.LENGTH_LONG).show();
}
});
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義復(fù)合控件實(shí)現(xiàn)通用標(biāo)題欄
- Android組合控件自定義標(biāo)題欄
- Android中自定義標(biāo)題欄樣式的兩種方法
- 3種Android隱藏頂部狀態(tài)欄及標(biāo)題欄的方法
- Android自定義狀態(tài)欄顏色與應(yīng)用標(biāo)題欄顏色一致
- Android中去掉標(biāo)題欄的幾種方法(三種)
- Android中隱藏標(biāo)題欄和狀態(tài)欄的方法
- Android 全屏無(wú)標(biāo)題欄的三種實(shí)現(xiàn)方法
- Android 頂部標(biāo)題欄隨滑動(dòng)時(shí)的漸變隱藏和漸變顯示效果
- Android自定義簡(jiǎn)單的頂部標(biāo)題欄
相關(guān)文章
Android Studio創(chuàng)建AIDL文件并實(shí)現(xiàn)進(jìn)程間通訊實(shí)例
本篇文章主要介紹了Android Studio創(chuàng)建AIDL文件并實(shí)現(xiàn)進(jìn)程間通訊實(shí)例,具有一定的參考價(jià)值,有興趣可以了解一下。2017-04-04
Android實(shí)現(xiàn)傾斜角標(biāo)樣式
最新小編接到這樣一個(gè)項(xiàng)目,需要在一個(gè)距形卡片上做一個(gè)傾斜的Tag,類(lèi)似支付寶上的一個(gè)功能,接著小編給大家?guī)?lái)了實(shí)現(xiàn)思路,對(duì)android 傾斜角標(biāo)的實(shí)現(xiàn)方法感興趣的朋友跟隨小編一起看看吧2019-10-10
Android 自動(dòng)化測(cè)試經(jīng)驗(yàn)分享 UiObejct.getFromParent()的使用方法
本篇文章對(duì)Android中UiObejct.getFromParent()的使用進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05
利用Android中BitmapShader制作自帶邊框的圓形頭像
這篇文章給大家介紹了一下如何利用BitmapShader制作圓形頭像,可以自定義要顯示的圖片,邊框顏色和邊框?qū)挾鹊?,有需要的朋友們可以參考借鑒。2016-09-09
Android實(shí)現(xiàn)自定義手勢(shì)和識(shí)別手勢(shì)的功能
這篇文章主要介紹了Android實(shí)現(xiàn)自定義手勢(shì)和識(shí)別手勢(shì)的功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
android 中ProgressDialog實(shí)現(xiàn)全屏效果的示例
本篇文章主要介紹了android 中ProgressDialog實(shí)現(xiàn)全屏效果的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
Android利用Fragment實(shí)現(xiàn)Tab選項(xiàng)卡效果
這篇文章主要為大家詳細(xì)介紹了Android利用Fragment實(shí)現(xiàn)Tab選項(xiàng)卡效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08

