我先上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" android:src="@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" 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" tools:visibility="visible" android:visibility="gone" /> <!-- 文字 --> <TextView android:id="@+id/baselayout_tv_left" android:gravity="center" android:maxWidth="240dip" android:singleLine="true" 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" 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,就有很大的通用性