为了账号安全,请及时绑定邮箱和手机立即绑定

如何写一个app通用的title

标签:
Android

我先上xml:

<?xml version="1.0" encoding="utf-8"?>
<merge
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="@dimen/title_height">
    <!-- 图标  -->
    <ImageView
        android:id="@+id/baselayout_iv_left"
        android:layout_width="wrap_content"
        android:layout_centerVertical="true"
        android:scaleType="center"
        style="@style/title_bar_common_style"
        android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@drawable/nav_btn_back" />

    <TextView
        android:id="@+id/baselayout_tv_title"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:singleLine="true"
        android:maxWidth="240dip"
        android:text="@string/app_name"
        style="@style/title_bar_common_style"
        android:textColor="@color/white_a"
        android:textSize="@dimen/text_size_20" />

    <ImageView
        android:id="@+id/baselayout_iv_right"
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:scaleType="center"
        style="@style/title_bar_common_style"
        tools:visibility="visible"
        android:visibility="gone" />

    <!-- 文字 -->
    <TextView
        android:id="@+id/baselayout_tv_left"
        android:gravity="center"
        android:maxWidth="240dip"
        android:singleLine="true"
        style="@style/title_bar_common_style"
        android:textColor="@color/white_a"
        android:textSize="@dimen/text_size_16"
        android:visibility="gone"
        tools:visibility="visible"/>

    <TextView
        android:id="@+id/baselayout_tv_right_yunqi"
        android:layout_alignParentRight="true"
        android:gravity="right|center_vertical"
        android:maxWidth="240dip"
        android:singleLine="true"
        style="@style/title_bar_common_style"
        android:textColor="@color/white_a"
        android:textSize="@dimen/text_size_16"
        android:visibility="visible" />

</merge>

然后上代码:

public class TitleBarCommon extends RelativeLayout {
    private TextView mTvTitle, mTvLeft, mTvRight;
    private ImageView mIvLeft, mIvRight;
    private View mTitleContainer;
    private Context context;

    public TitleBarCommon(Context context) {
        this(context, null);

    }

    public TitleBarCommon(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        this.mTitleContainer = LayoutInflater.from(context).inflate( R.layout.activity_base_header_common, this,true);
        init();
    }


    public void init() {
        mTvTitle = (TextView) mTitleContainer.findViewById(R.id.baselayout_tv_title);
        mIvLeft = (ImageView) mTitleContainer.findViewById(R.id.baselayout_iv_left);
        mTvLeft = (TextView) mTitleContainer.findViewById(R.id.baselayout_tv_left);
        mIvRight = (ImageView) mTitleContainer.findViewById(R.id.baselayout_iv_right);
        mTvRight = (TextView) mTitleContainer.findViewById(R.id.baselayout_tv_right_yunqi);
    }

    private LayoutInflater mLayoutInflater;
    public void setLayoutInflater(LayoutInflater inflater){
        mLayoutInflater = inflater;
    }
    /**自定义头部**/
    public void setCustomTitleBar(int customTitleLayoutId){
        if (customTitleLayoutId <=0) {
            this.setVisibility(View.GONE);
            return ;
        }
        removeAllViews();
        View baseHead;
        if(mLayoutInflater!=null){
            baseHead = mLayoutInflater.inflate(customTitleLayoutId, null);
        }else{
            baseHead = View.inflate(context,customTitleLayoutId, null);
        }
        RelativeLayout.LayoutParams params=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        addView(baseHead,params);
    }

    /**自定义头部**/
    public void setCustomTitleBar(View baseHead){
        if (baseHead == null) {
            this.setVisibility(View.GONE);
            return ;
        }
        removeAllViews();
//        View baseHead = View.inflate(context,customTitleLayoutId, null);
        LayoutParams params=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        addView(baseHead,params);
    }

    /**
     * 右TextView 文字
     *
     * @param
     * @return
     */
    public TitleBarCommon setRightTextViewString(String text) {
        if (!TextUtils.isEmpty(text)) {
            mTvRight.setText(text);
            mTvRight.setVisibility(View.VISIBLE);
        } else {
            mTvRight.setVisibility(View.GONE);
        }
        return this;
    }

    /**
     * 设置右TextView点击事件
     *
     * @param lsnr
     * @return
     */
    public TitleBarCommon setRightTextViewListener(OnClickListener lsnr) {
        mTvRight.setOnClickListener(lsnr);
        return this;
    }

}

这样使用这个title,我们看一个activity的layout:

<?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"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="@color/white_a"
              xmlns:tools="http://schemas.android.com/tools"
              android:orientation="vertical">

    <com.pic.optimize.view.TitleBarCommon
        android:id="@+id/head_common_layout"
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:background="@color/red_bn"
        android:gravity="center_vertical"/>

    <ListView
        android:id="@+id/news_home_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@null"
        android:fadingEdge="none"
        android:footerDividersEnabled="true"
        android:listSelector="@color/trans_color"
        android:scrollbars="none"
        android:scrollingCache="false" />

</LinearLayout>
private void initTitle() {
    TitleBarCommon titleBarCommon = (TitleBarCommon)findViewById(R.id.head_common_layout);
    titleBarCommon.setRightTextViewString("增加");
    titleBarCommon.setRightTextViewListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(TestHttpActivity.this,"aaa",Toast.LENGTH_LONG).show();
        }
    });
}

从这个代码可以看出几个有意思的地方,因为这个view本身继承RelateviewLayout,所以根节点为merge,这样可以少一层嵌套

还有看一下方法setCustomTitleBar,这个方法去除了这个view本身的layout,然后可以set自己的layout,就有很大的通用性

代码在:https://github.com/nickgao1986/StepSport

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消