前言
标题栏这个部分,绝大多数App都有,起到一个概括本页面功能的作用,有些标题栏还具有返回上一页等功能。标题栏是Android开发最基础的一个组件,但是应用也多种多样,因为应对需求的多样化,想要做一个万能的标题栏基本上,哈,是不可能的,因此跟大家分享一下自己简易封装的标题栏,含金量不大,为了以后方便开发和使用,顺便记录下来留作分享!
其实很好理解,就是左图,左标题,标题,右标题,右图的一些属性设置。
一. 创建attrs.xml
<?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>
主要设置文字内容,文字,颜色,图片资源样式
二. 创建dimens文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--标题栏的高度-->
<dimen name="title_height">40dp</dimen>
<dimen name="status_bar_height">0dp</dimen>
<!--常用的文字大小-->
<dimen name="text_size_10">10sp</dimen>
<dimen name="text_size_12">12sp</dimen>
<dimen name="text_size_14">14sp</dimen>
<dimen name="text_size_16">16sp</dimen>
<dimen name="text_size_18">18sp</dimen>
<dimen name="text_size_20">20sp</dimen>
<dimen name="text_size_24">24sp</dimen>
<dimen name="text_size_32">32sp</dimen>
<dimen name="text_size_48">48sp</dimen>
<dimen name="text_size_64">64sp</dimen>
<!-- 字体标准大小规范 -->
<dimen name="font_size1">12sp</dimen>
<dimen name="font_size2">12sp</dimen>
<dimen name="font_size3">14sp</dimen>
<dimen name="font_size4">14sp</dimen>
<dimen name="font_size5">16sp</dimen>
<dimen name="font_size6">16sp</dimen>
<dimen name="font_size7">16sp</dimen>
<dimen name="font_size8">16sp</dimen>
<dimen name="font_size9">16sp</dimen>
<dimen name="font_size10">16sp</dimen>
<!--常用的间距-->
<dimen name="size_2">2dp</dimen>
<dimen name="size_4">4dp</dimen>
<dimen name="size_6">6dp</dimen>
<dimen name="size_8">8dp</dimen>
<dimen name="size_12">12dp</dimen>
<dimen name="size_16">16dp</dimen>
<dimen name="size_20">20dp</dimen>
<dimen name="size_24">24dp</dimen>
<dimen name="size_30">30dp</dimen>
<dimen name="size_32">32dp</dimen>
<dimen name="size_40">40dp</dimen>
<dimen name="size_48">48dp</dimen>
<dimen name="size_60">60dp</dimen>
<dimen name="size_64">64dp</dimen>
<dimen name="size_72">72dp</dimen>
<dimen name="size_96">96dp</dimen>
<dimen name="size_128">128dp</dimen>
<dimen name="size_144">144dp</dimen>
<dimen name="size_160">160dp</dimen>
<dimen name="size_172">172dp</dimen>
<dimen name="size_224">224dp</dimen>
<dimen name="size_256">256dp</dimen>
<dimen name="size_320">320dp</dimen>
<dimen name="size_640">640dp</dimen>
<!-- 视频聊天中小窗口移动时的阈值 -->
<dimen name="video_smallview_move_thresholdX">30dp</dimen>
<dimen name="video_smallview_move_thresholdY">40dp</dimen>
<dimen name="small_area_margin_bottom">110dp</dimen>
<dimen name="small_area_margin_top">130dp</dimen>
<dimen name="small_area_height">110dp</dimen>
<dimen name="small_area_width">60dp</dimen>
<dimen name="small_area_marginright">10dp</dimen>
<dimen name="small_area_marginbetween">10dp</dimen>
<dimen name="qav_title_margin_top">3dp</dimen>
<dimen name="qav_title_bar_height">50dp</dimen>
<dimen name="video_small_view_width">120dp</dimen>
<dimen name="video_small_view_height">160dp</dimen>
<dimen name="video_small_view_offsetX">10dp</dimen>
<dimen name="video_small_view_offsetY">10dp</dimen>
<dimen name="video_small_video_margin">1.5dp</dimen>
<dimen name="video_small_mute_margin">5dp</dimen>
<dimen name="video_msgbox_offsetX">23dp</dimen>
<dimen name="video_msgbox_offsetY">3dp</dimen>
<dimen name="video_msgbox_offset">3dp</dimen>
<dimen name="video_title_default_width">200dp</dimen>
<dimen name="video_top_toolbar_margin">45dp</dimen>
<dimen name="video_bottom_toolbar_margin">10dp</dimen>
<!-- 距离定义规范 -->
<dimen name="d_150">-75dp</dimen>
<dimen name="d_60">-30dp</dimen>
<dimen name="d_8">-4dp</dimen>
<dimen name="d_5">-3dp</dimen>
<dimen name="d_2">-1dp</dimen>
<dimen name="d1">1dp</dimen>
<dimen name="d2">1dp</dimen>
<dimen name="d3">2dp</dimen>
<dimen name="d4">2dp</dimen>
<dimen name="d5">3dp</dimen>
<dimen name="d6">3dp</dimen>
<dimen name="d7">4dp</dimen>
<dimen name="d8">4dp</dimen>
<dimen name="d9">5dp</dimen>
<dimen name="d10">5dp</dimen>
<dimen name="d11">6dp</dimen>
<dimen name="d12">6dp</dimen>
<dimen name="d13">7dp</dimen>
<dimen name="d14">7dp</dimen>
<dimen name="d15">8dp</dimen>
<dimen name="d16">8dp</dimen>
<dimen name="d17">9dp</dimen>
<dimen name="d18">9dp</dimen>
<dimen name="d19">10dp</dimen>
<dimen name="d20">10dp</dimen>
<dimen name="d21">11dp</dimen>
<dimen name="d22">11dp</dimen>
<dimen name="d23">12dp</dimen>
<dimen name="d24">12dp</dimen>
<dimen name="d25">13dp</dimen>
<dimen name="d26">13dp</dimen>
<dimen name="d27">14dp</dimen>
<dimen name="d28">14dp</dimen>
<dimen name="d29">15dp</dimen>
<dimen name="d30">15dp</dimen>
<dimen name="d31">16dp</dimen>
<dimen name="d32">16dp</dimen>
<dimen name="d33">17dp</dimen>
<dimen name="d34">17dp</dimen>
<dimen name="d35">18dp</dimen>
<dimen name="d36">18dp</dimen>
<dimen name="d37">19dp</dimen>
<dimen name="d38">19dp</dimen>
<dimen name="d39">20dp</dimen>
<dimen name="d40">20dp</dimen>
<dimen name="d41">21dp</dimen>
<dimen name="d42">21dp</dimen>
<dimen name="d43">22dp</dimen>
<dimen name="d44">22dp</dimen>
<dimen name="d45">23dp</dimen>
<dimen name="d46">23dp</dimen>
<dimen name="d47">24dp</dimen>
<dimen name="d48">24dp</dimen>
<dimen name="d49">25dp</dimen>
<dimen name="d50">25dp</dimen>
<dimen name="d51">26dp</dimen>
<dimen name="d52">26dp</dimen>
<dimen name="d53">27dp</dimen>
<dimen name="d54">27dp</dimen>
<dimen name="d55">28dp</dimen>
<dimen name="d56">28dp</dimen>
<dimen name="d57">29dp</dimen>
<dimen name="d58">29dp</dimen>
<dimen name="d59">30dp</dimen>
<dimen name="d60">30dp</dimen>
<dimen name="d61">31dp</dimen>
<dimen name="d62">31dp</dimen>
<dimen name="d63">32dp</dimen>
<dimen name="d64">32dp</dimen>
<dimen name="d65">33dp</dimen>
<dimen name="d66">33dp</dimen>
<dimen name="d67">34dp</dimen>
<dimen name="d68">34dp</dimen>
<dimen name="d69">35dp</dimen>
<dimen name="d70">35dp</dimen>
<dimen name="d71">36dp</dimen>
<dimen name="d72">36dp</dimen>
<dimen name="d73">37dp</dimen>
<dimen name="d74">37dp</dimen>
<dimen name="d75">38dp</dimen>
<dimen name="d76">38dp</dimen>
<dimen name="d77">39dp</dimen>
<dimen name="d78">39dp</dimen>
<dimen name="d79">40dp</dimen>
<dimen name="d80">40dp</dimen>
<dimen name="d81">41dp</dimen>
<dimen name="d82">41dp</dimen>
<dimen name="d83">42dp</dimen>
<dimen name="d84">42dp</dimen>
<dimen name="d85">43dp</dimen>
<dimen name="d86">43dp</dimen>
<dimen name="d87">44dp</dimen>
<dimen name="d88">44dp</dimen>
<dimen name="d89">45dp</dimen>
<dimen name="d90">45dp</dimen>
<dimen name="d91">46dp</dimen>
<dimen name="d92">46dp</dimen>
<dimen name="d93">47dp</dimen>
<dimen name="d94">47dp</dimen>
<dimen name="d95">48dp</dimen>
<dimen name="d96">48dp</dimen>
<dimen name="d97">49dp</dimen>
<dimen name="d98">49dp</dimen>
<dimen name="d99">50dp</dimen>
<dimen name="d100">50dp</dimen>
<dimen name="d110">55dp</dimen>
<dimen name="d120">60dp</dimen>
<dimen name="d128">64dp</dimen>
<dimen name="d130">65dp</dimen>
<dimen name="d135">68dp</dimen>
<dimen name="d145">73dp</dimen>
<dimen name="d150">75dp</dimen>
<dimen name="d155">78dp</dimen>
<dimen name="d160">80dp</dimen>
<dimen name="d164">82dp</dimen>
<dimen name="d165">83dp</dimen>
<dimen name="d175">88dp</dimen>
<dimen name="d180">90dp</dimen>
<dimen name="d195">98dp</dimen>
<dimen name="d200">100dp</dimen>
<dimen name="d204">102dp</dimen>
<dimen name="d220">110dp</dimen>
<dimen name="d235">118dp</dimen>
<dimen name="d240">120dp</dimen>
<dimen name="d250">125dp</dimen>
<dimen name="d290">145dp</dimen>
<dimen name="d300">150dp</dimen>
<dimen name="d330">165dp</dimen>
<dimen name="d350">175dp</dimen>
<dimen name="d370">185dp</dimen>
<dimen name="d380">190dp</dimen>
<dimen name="d400">200dp</dimen>
<dimen name="d423">212dp</dimen>
<dimen name="d450">225dp</dimen>
<dimen name="d460">230dp</dimen>
<dimen name="d500">250dp</dimen>
<dimen name="d600">300dp</dimen>
<dimen name="d640">320dp</dimen>
</resources>
三. 自定义titlebar布局
<?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
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() {
@Override
public void onClick(View view) {
mClick.leftClick();
}
});
layout_right.setOnClickListener(new OnClickListener() {
@Override
public 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();
}
}
五. 布局中直接引用自定义的view
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.testing.mycustomview.MainActivity">
<com.example.testing.mycustomview.TitlebarView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/colorAccent"
app:centerTextColor="#FFF"
app:centerTitle="自定义标题"
app:leftDrawble="@drawable/ic_write_back"
app:leftText="返回"
app:leftTextColor="#FFF"
app:rightDrawable="@android:drawable/ic_btn_speak_now"
app:rightText="语音"
app:rightTextColor="#FFF" />
</LinearLayout>
可以直接在布局文件中直接设置图标和文字以及字体颜色,当然,也可以直接在代码中设置
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();//初始化titlebar
}
private void init() {
TitlebarView titlebarView = (TitlebarView) findViewById(R.id.title);
titlebarView.setTitleSize(20);//标题大小
titlebarView.setLeftTextSize(18);//左侧文字大小
titlebarView.setRightTextSize(18);//右侧文字大小
//titlebarView.setLeftDrawable("图标");左侧图标
//titlebarView.setRightDrawable("图标");//右侧图标
titlebarView.setBackgroundResource(R.color.colorAccent);//titlebar背景
titlebarView.setTitle("标题栏");//标题
titlebarView.setRightText("提交");//右侧文字
titlebarView.setLeftText("返回");//左侧文字
//点击事件
titlebarView.setOnViewClick(new TitlebarView.onViewClick() {
@Override
public void leftClick() {
Toast.makeText(MainActivity.this, "左边", Toast.LENGTH_SHORT).show();
}
@Override
public void rightClick() {
Toast.makeText(MainActivity.this, "右边", Toast.LENGTH_SHORT).show();
}
});
}
}
欧克,至此,分享结束,过段时间,过段时间再优化一下,已经有了一个新的想法,正在试着呢!