实现后的样式为:
定义XML布局:(navigation_bar.xml)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/title_bar" style="@style/title_bar_style"> <LinearLayout android:id="@+id/top_left_container" android:layout_width="50dip" android:layout_height="fill_parent" android:paddingLeft="10.0dip" android:paddingRight="10.0dip"> <ImageView android:id="@+id/top_left" android:layout_width="19dip" android:layout_height="19dip" android:layout_gravity="center_vertical" android:padding="2.0dip" android:src="@mipmap/top_navigation_back" /> </LinearLayout> <LinearLayout android:id="@+id/top_right_container" android:layout_width="50dip" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="10dip" android:paddingLeft="10.0dip" android:paddingRight="10.0dip"> <ImageView android:id="@+id/top_search" android:layout_width="wrap_content" android:layout_height="fill_parent" android:gravity="center" android:scaleType="centerInside" android:src="@mipmap/search_icon" /> </LinearLayout> <TextView android:id="@+id/top_title" style="@style/title_text_style" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dip" android:layout_toRightOf="@+id/top_left_container" android:paddingBottom="1.5dip" android:paddingLeft="6.0dip" android:paddingRight="6.0dip" android:text="@string/app_name" /> </RelativeLayout>
第二步:
自定义要用到的控件的属性:(文件在res/values/attrs.xml)
<declare-styleable name="TopNavigationBar"> <attr name="topbarTitle" format="string"></attr> <attr name="leftImBtnDrawable" format="reference"></attr> <attr name="rightImBtnDrawable" format="reference"></attr> <attr name="hideLeftImBtn" format="boolean" /> <attr name="hideRightImBtn" format="boolean" /> </declare-styleable>
第三步:
处理自定义控件的事件
public class TopNavigationBar extends FrameLayout implements View.OnClickListener { private TextView mTopTitle; private LinearLayout mLeftContainerLl; private LinearLayout mRightContainerLl; private LeftBackClickListener leftBackClickListener; private RightSearchClickListener rightSearchClickListener; public TopNavigationBar(Context context, AttributeSet attrs) { this(context, attrs, 0); } public TopNavigationBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TopNavigationBar); LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.navigation_bar, this, true); CharSequence title = a.getText(R.styleable.TopNavigationBar_topbarTitle); mTopTitle = (TextView) findViewById(R.id.top_title); mLeftContainerLl = (LinearLayout) findViewById(R.id.top_left_container); mRightContainerLl = (LinearLayout) findViewById(R.id.top_right_container); mLeftContainerLl.setOnClickListener(this); mRightContainerLl.setOnClickListener(this); mTopTitle.setText(title); boolean isHidedLeftImBtn = a.getBoolean(R.styleable.TopNavigationBar_hideLeftImBtn, false); if (isHidedLeftImBtn) { mLeftContainerLl.setVisibility(GONE); } boolean isHidedRightImBtn = a.getBoolean(R.styleable.TopNavigationBar_hideRightImBtn, false); if (isHidedRightImBtn) { mRightContainerLl.setVisibility(GONE); } a.recycle(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.top_left_container: { leftBackClickListener.onLeftBtnClick(); break; } case R.id.top_right_container: { rightSearchClickListener.onRightClick(); break; } default: break; } } public void hindRight(){ mRightContainerLl.setVisibility(GONE); } public void hindLeft(){ mLeftContainerLl.setVisibility(GONE); } public void showRight(){ mRightContainerLl.setVisibility(VISIBLE); } public void showLeft(){ mLeftContainerLl.setVisibility(VISIBLE); } public void setOnLeftBackListener(LeftBackClickListener leftBackListener){ this.leftBackClickListener = leftBackListener; } public void setOnRightSearchListener(RightSearchClickListener rightSearchListener){ this.rightSearchClickListener = rightSearchListener; } public interface LeftBackClickListener{ void onLeftBtnClick(); } public interface RightSearchClickListener{ void onRightClick(); } }
第四步:
使用该自定义的控件:
<com.luoshutao.mygraduateproject.utils.TopNavigationBar android:id="@+id/top_bar" android:layout_width="fill_parent" android:layout_height="wrap_content" add:hideLeftImBtn="false" add:hideRightImBtn="true" add:leftImBtnDrawable="@mipmap/top_navigation_back" add:topbarTitle="找回成功"> </com.luoshutao.mygraduateproject.utils.TopNavigationBar>