UI的Title
项⽬中,每个页⾯基本上都会有title,系统默认的actionbar⼀般都没⼈⽤,⼤部分时候是⾃⼰进⾏定义。这⾥写⼀个通⽤的Titile模块,⽅便以后在其他APP中使⽤。
⾸先,是布局,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="schemas.android/apk/res/android" android:id="@+id/rl_titlebar"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/white"
android:orientation="horizontal" >
<ImageView
android:id="@+id/titlebar_iv_left"
android:layout_width="48dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:padding="12dp"
android:visibility="gone" />
<TextView
android:id="@+id/titlebar_tv_left"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="@drawable/txtcolor_oran2alpha_sel"
android:textSize="@dimen/txtsize_subhead"
android:visibility="gone" />
<TextView
android:id="@+id/titlebar_tv"
android:layout_centerInParent="true"
android:gravity="center"
android:maxWidth="224dp" />
<ImageView
android:id="@+id/titlebar_iv_right"
android:layout_width="48dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:padding="12dp"
android:visibility="gone" />
<TextView
android:id="@+id/titlebar_tv_right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="@drawable/txtcolor_oran2alpha_sel"
android:textSize="@dimen/txtsize_subhead"
android:visibility="gone" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_alignParentBottom="true"
android:background="@color/bg_agray" />
</RelativeLayout>
基本上就是中间是title⽂字,左边和右边也有⽂字或者按钮,默认都是gone状态,需要的时候再显⽰出来,在其他布局中,通过include标签加载进去即可:
<include layout="@layout/include_titlebar"/>
最后,我们在代码中设置调⽤即可,这⾥我们进⾏设置的Util⼯具类进⾏封装,使⽤Builder模式,具体代码如下:
import android.app.Activity;
TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import com.fanxl.fwbo.R;
public class TitleBuilder {
private View viewTitle;
private TextView tvTitle;
private ImageView ivLeft;
private ImageView ivRight;
private TextView tvLeft;
private TextView tvRight;
public TitleBuilder(Activity context) {
viewTitle = context.findViewById(R.id.rl_titlebar);
init();
}
public TitleBuilder(View context) {
viewTitle = context.findViewById(R.id.rl_titlebar);
init();
}
private void init(){
tvTitle = (TextView) viewTitle.findViewById(R.id.titlebar_tv);
ivLeft = (ImageView) viewTitle.findViewById(R.id.titlebar_iv_left);
ivRight = (ImageView) viewTitle.findViewById(R.id.titlebar_iv_right);
tvLeft = (TextView) viewTitle.findViewById(R.id.titlebar_tv_left);
tvRight = (TextView) viewTitle.findViewById(R.id.titlebar_tv_right);
}
//设置标题的背景颜⾊
public TitleBuilder setTitleBgRes(int resid) {
viewTitle.setBackgroundResource(resid);
return this;
}
//设置标题⽂字
public TitleBuilder setTitleText(String text) {
tvTitle.setVisibility(TextUtils.isEmpty(text) ? View.GONE
: View.VISIBLE);
tvTitle.setText(text);
return this;
}
//设置左边图⽚
public TitleBuilder setLeftImage(int resId) {
ivLeft.setVisibility(resId > 0 ? View.VISIBLE : View.GONE);
ivLeft.setImageResource(resId);
return this;
}
//设置左边⽂字
public TitleBuilder setLeftText(String text) {
tvLeft.setVisibility(TextUtils.isEmpty(text) ? View.GONE : View.VISIBLE);
tvLeft.setText(text);
return this;
}
//设置左边显⽰空间的点击事件
public TitleBuilder setLeftOnClickListener(OnClickListener listener) {
if (Visibility() == View.VISIBLE) {
ivLeft.setOnClickListener(listener);
} else if (Visibility() == View.VISIBLE) {
tvLeft.setOnClickListener(listener);
}
return this;
}
// right
public TitleBuilder setRightImage(int resId) {
ivRight.setVisibility(resId > 0 ? View.VISIBLE : View.GONE);
ivRight.setImageResource(resId);
return this;
}
public TitleBuilder setRightText(String text) {
tvRight.setVisibility(TextUtils.isEmpty(text) ? View.GONE
: View.VISIBLE);
tvRight.setText(text);
return this;
}
public TitleBuilder setRightOnClickListener(OnClickListener listener) {
if (Visibility() == View.VISIBLE) {
ivRight.setOnClickListener(listener);
} else if (Visibility() == View.VISIBLE) {
tvRight.setOnClickListener(listener);
}
return this;
}
public View build() {
return viewTitle;
}
}
代码中使⽤:
new TitleBuilder(view)
view ui框架.setTitleText("⾸页")
.setLeftText("左边")
.
setLeftOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ToastUtils.showToast(activity, "left onclick", Toast.LENGTH_SHORT);
}
});
设置标题为⾸页,设置左边⽂字,并设置点击事件弹出吐司。
这样,在其他页⾯使⽤,只需要在xml中include⼀下布局,然后在代码中设置⼀下即可使⽤。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。