日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android封装一个自定义标题栏

發布時間:2023/12/20 Android 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android封装一个自定义标题栏 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

標題欄是Android開發最基礎的一個組件,但是應用也多種多樣,因為應對需求的多樣化,想要做一個萬能的標題欄基本是不可能,因此跟大家分享一下自己簡易封裝的標題欄,并不具備多大含金量,應對于以下一些樣式。

1:

2:

3:

4:

5:

其實很好理解,就是左圖,左標題,標題,右標題,右圖的一些屬性設置。

第一步:

在values下面新建attrs文件,設置屬性

<?xml version="1.0" encoding="utf-8"?> <resources><declare-styleable name="TitlebarView"><attr name="leftText" format="string"/><attr name="leftTextColor" format="color"/><attr name="centerTextColor" format="color"/><attr name="centerTitle" format="string"/><attr name="rightTextColor" format="color"/><attr name="rightText" format="string"/><attr name="leftDrawble" format="reference"/><attr name="rightDrawable" format="reference"/></declare-styleable> </resources>

主要設置內容,顏色,圖片等

第二步:

設置標題欄的布局文件

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/layout_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@android:color/transparent"android:orientation="vertical"><LinearLayout android:id="@+id/layout_left"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_alignParentLeft="true"android:gravity="center"android:orientation="horizontal"android:paddingLeft="@dimen/size_8"android:paddingRight="@dimen/size_8"><ImageView android:id="@+id/iv_left"android:layout_width="wrap_content"android:layout_height="wrap_content"android:contentDescription="@null"android:scaleType="centerInside" /><TextView android:id="@+id/tv_left"android:layout_width="wrap_content"android:layout_height="wrap_content"android:maxLength="4"android:maxLines="1"android:paddingLeft="@dimen/size_8" /></LinearLayout><TextView android:id="@+id/tv_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:gravity="center"android:maxLength="10"android:maxLines="1" /><LinearLayout android:id="@+id/layout_right"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_alignParentRight="true"android:gravity="center"android:paddingLeft="@dimen/size_8"android:paddingRight="@dimen/size_8"><TextView android:id="@+id/tv_right"android:layout_width="wrap_content"android:layout_height="wrap_content"android:maxLines="1" /><ImageView android:id="@+id/iv_right"android:layout_width="wrap_content"android:layout_height="wrap_content"android:contentDescription="@null"android:scaleType="centerInside" /></LinearLayout> </RelativeLayout>

這里的布局文件是通用的布局文件,需要對此布局根據自己的需求進行配置,這個就需要自定義View了

第三步:

自定義TitleBar,主要進行引用布局,初始化view,拿到自定義屬性進行相應配置

public TitlebarView(final Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);LayoutInflater.from(context).inflate(R.layout.layout_title, this);initView();TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TitlebarView, defStyleAttr, 0);int count = array.getIndexCount();for (int i = 0; i < count; i++) {int attr = array.getIndex(i);switch (attr) {case R.styleable.TitlebarView_leftTextColor:tv_left.setTextColor(array.getColor(attr, Color.BLACK));break;case R.styleable.TitlebarView_leftDrawble:iv_left.setImageResource(array.getResourceId(attr, 0));break;case R.styleable.TitlebarView_leftText:tv_left.setText(array.getString(attr));break;case R.styleable.TitlebarView_centerTextColor:tv_title.setTextColor(array.getColor(attr, Color.BLACK));break;case R.styleable.TitlebarView_centerTitle:tv_title.setText(array.getString(attr));break;case R.styleable.TitlebarView_rightDrawable:iv_right.setImageResource(array.getResourceId(attr, 0));break;case R.styleable.TitlebarView_rightText:tv_right.setText(array.getString(attr));break;case R.styleable.TitlebarView_rightTextColor:tv_right.setTextColor(array.getColor(attr, Color.BLACK));break;}}array.recycle();}

然后我們需要添加左側,右側的點擊事件,由于一般反饋點擊范圍太小,導致點擊靈敏性較差,所以這里全部設置對左側,右側布局進行監聽,而不是單個控件

設置回調接口

private onViewClick mClick;public void setOnViewClick(onViewClick click) {this.mClick = click;}public interface onViewClick {void leftClick();void rightClick();}

在調用的時候拿到自定義view對象setOnViewClick即可

全部代碼如下:

package com.example.testing.mycustomview;import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.text.TextUtils; import android.util.AttributeSet; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView;/*** Created by Administrator on 2017/7/18.*/public class TitlebarView extends RelativeLayout {private LinearLayout layout_left, layout_right;private TextView tv_left, tv_title, tv_right;private ImageView iv_left, iv_right;private onViewClick mClick;public TitlebarView(Context context) {this(context, null);}public TitlebarView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public TitlebarView(final Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);LayoutInflater.from(context).inflate(R.layout.layout_title, this);initView();TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TitlebarView, defStyleAttr, 0);int count = array.getIndexCount();for (int i = 0; i < count; i++) {int attr = array.getIndex(i);switch (attr) {case R.styleable.TitlebarView_leftTextColor:tv_left.setTextColor(array.getColor(attr, Color.BLACK));break;case R.styleable.TitlebarView_leftDrawble:iv_left.setImageResource(array.getResourceId(attr, 0));break;case R.styleable.TitlebarView_leftText:tv_left.setText(array.getString(attr));break;case R.styleable.TitlebarView_centerTextColor:tv_title.setTextColor(array.getColor(attr, Color.BLACK));break;case R.styleable.TitlebarView_centerTitle:tv_title.setText(array.getString(attr));break;case R.styleable.TitlebarView_rightDrawable:iv_right.setImageResource(array.getResourceId(attr, 0));break;case R.styleable.TitlebarView_rightText:tv_right.setText(array.getString(attr));break;case R.styleable.TitlebarView_rightTextColor:tv_right.setTextColor(array.getColor(attr, Color.BLACK));break;}}array.recycle();layout_left.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {mClick.leftClick();}});layout_right.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {mClick.rightClick();}});}private void initView() {tv_left = findViewById(R.id.tv_left);tv_title = findViewById(R.id.tv_title);tv_right = findViewById(R.id.tv_right);iv_left = findViewById(R.id.iv_left);iv_right = findViewById(R.id.iv_right);layout_left = findViewById(R.id.layout_left);layout_right = findViewById(R.id.layout_right);}public void setOnViewClick(onViewClick click) {this.mClick = click;}//設置標題public void setTitle(String title) {if (!TextUtils.isEmpty(title)) {tv_title.setText(title);}}//設置左標題public void setLeftText(String title) {if (!TextUtils.isEmpty(title)) {tv_left.setText(title);}}//設置右標題public void setRightText(String title) {if (!TextUtils.isEmpty(title)) {tv_right.setText(title);}}//設置標題大小public void setTitleSize(int size) {if (tv_title != null) {tv_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);}}//設置左標題大小public void setLeftTextSize(int size) {if (tv_left != null) {tv_left.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);}}//設置右標題大小public void setRightTextSize(int size) {if (tv_right != null) {tv_right.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);}}//設置左圖標public void setLeftDrawable(int res) {if (iv_left != null) {iv_left.setImageResource(res);}}//設置右圖標public void setRightDrawable(int res) {if (iv_right != null) {iv_right.setImageResource(res);}}public interface onViewClick {void leftClick();void rightClick();}}

如何引用呢?

<com.example.testing.mycustomview.TitlebarViewandroid:id="@+id/title"android:layout_width="match_parent"android:layout_height="48dp"android:background="@color/colorAccent"app:centerTitle="自定義標題"app:centerTextColor="#FFF"app:leftDrawble="@drawable/ic_write_back"app:leftTextColor="#FFF"app:leftText="返回"app:rightDrawable="@android:drawable/ic_btn_speak_now"app:rightText="語音"app:rightTextColor="#FFF"/>

效果:

代碼中調用:

TitlebarView titlebarView= (TitlebarView) findViewById(R.id.title);titlebarView.setTitleSize(20);titlebarView.setTitle("標題欄");titlebarView.setOnViewClick(new TitlebarView.onViewClick() {@Overridepublic void leftClick() {Toast.makeText(MainActivity.this,"左邊",Toast.LENGTH_SHORT).show();}@Overridepublic void rightClick() {Toast.makeText(MainActivity.this,"右邊",Toast.LENGTH_SHORT).show();}});

效果:

到這里就結束了,基本上可以滿足大多數需求

代碼鏈接:
http://pan.baidu.com/s/1qXH7nWg

總結

以上是生活随笔為你收集整理的Android封装一个自定义标题栏的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。