自定義標題欄XMl佈局文件android
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="75dp" android:background="@color/colorPrimaryDark" > <LinearLayout android:id="@+id/ll_home" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_centerVertical="true" android:layout_alignParentLeft="true"> <TextView android:id="@+id/title_left" android:layout_width="200px" android:layout_height="35px" android:layout_marginLeft="15px" android:layout_gravity="bottom" android:textSize="20sp" android:background="@mipmap/ic_launcher" android:textColor="#000000" /> </LinearLayout> <TextView android:id="@+id/title_center" android:text="標題" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_centerVertical="true" android:textSize="18sp" android:textColor="#ffff" /> <TextView android:id="@+id/title_inter" android:layout_width="60px" android:layout_height="60px" android:layout_centerVertical="true" android:layout_marginRight="5px" android:layout_toLeftOf="@+id/title_right" android:textColor="#000000" android:background="@mipmap/ic_launcher" android:textSize="20sp" /> /> <TextView android:id="@+id/title_right" android:layout_width="60px" android:layout_height="60px" android:layout_centerInParent="true" android:layout_alignParentRight="true" android:layout_marginRight="5px" android:textSize="20sp" android:textColor="#000000" android:background="@mipmap/ic_launcher" /> </RelativeLayout>
下面是封裝好的類ide
package com.example.biaotilan2; import android.annotation.TargetApi; import android.content.Context; import android.os.Build; import android.text.TextUtils; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; /** * Created by asus on 2017/11/20. */ public class MyAppTitle extends LinearLayout { private OnLeftButtonClickListener mLeftButtonClickListener; private OnRightButtonClickListener mRightButtonClickListener; private MyViewHolder mViewHolder; private View viewAppTitle; public MyAppTitle(Context context) { super(context); init(); } public MyAppTitle(Context context, AttributeSet attrs) { super(context, attrs); init(); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) public MyAppTitle(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); //獲取佈局 viewAppTitle = inflater.inflate(R.layout.home_title, null); this.addView(viewAppTitle, layoutParams); mViewHolder = new MyViewHolder(this); //點擊事件 mViewHolder.title_inter.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mLeftButtonClickListener != null) { mLeftButtonClickListener.onLeftButtonClick(v); } } }); //點擊事件 mViewHolder.title_right.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mRightButtonClickListener != null) { mRightButtonClickListener.OnRightButtonClick(v); } } }); } //設置標題欄的控件顯示與否 public void initViewsVisible(boolean isLeftButtonVisile, boolean isCenterTitleVisile, boolean isRightIconVisile, boolean isRightTitleVisile) { // 左側返回 mViewHolder.ll_home.setVisibility(isLeftButtonVisile ? View.VISIBLE : View.INVISIBLE); // 中間標題 mViewHolder.title_center.setVisibility(isCenterTitleVisile ? View.VISIBLE : View.INVISIBLE); //右側兩個 mViewHolder.title_inter.setVisibility(isRightIconVisile ? View.VISIBLE : View.INVISIBLE); mViewHolder.title_right.setVisibility(isRightTitleVisile ? View.VISIBLE : View.INVISIBLE); } /** * 設置標題 * * @param title */ public void setAppTitle(String title) { if (!TextUtils.isEmpty(title)) { mViewHolder.title_center.setText(title); } } public void setRightTitle(String text) { if (!TextUtils.isEmpty(text)) { mViewHolder.title_center.setText(text); } } public void setRightIcon(int sourceID) { } public void setLeftOnclick(int sourceID) { mViewHolder.title_inter.setText(sourceID); } public void setAppBackground(int color) { viewAppTitle.setBackgroundColor(color); } public void setOnLeftButtonClickListener(OnLeftButtonClickListener listen) { mLeftButtonClickListener = listen; } public void setOnRightButtonClickListener(OnRightButtonClickListener listen) { mRightButtonClickListener = listen; } public static abstract interface OnLeftButtonClickListener { public abstract void onLeftButtonClick(View v); } public static abstract interface OnRightButtonClickListener { public abstract void OnRightButtonClick(View v); } //獲取id static class MyViewHolder { private final LinearLayout ll_home; private final TextView title_center; private final TextView title_inter; private final TextView title_right; public MyViewHolder(View v) { ll_home = v.findViewById(R.id.ll_home); title_center = v.findViewById(R.id.title_center); title_inter = v.findViewById(R.id.title_inter); title_right = v.findViewById(R.id.title_right); } } }
在使用中如何使用呢,接下來看在項目中 佈局文件的引用佈局
足以包名修改ui
<com.example.biaotilan2.MyAppTitle android:id="@+id/myNewAppTitle" android:layout_width="match_parent" android:layout_height="50dp"></com.example.biaotilan2.MyAppTitle>
下面是在activity中如何初始化該標題的代碼this
public class MainActivity extends AppCompatActivity { MyAppTitle mNewAppTitle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //調用個方法 setMyAppTitle(); } protected void setMyAppTitle(){ //獲取封裝類 mNewAppTitle = (MyAppTitle) this.findViewById(R.id.myNewAppTitle); //設置你的控件顯示與否 mNewAppTitle.initViewsVisible(true, true, true, true); //設置標題 mNewAppTitle.setAppTitle("啦啦啦"); 點擊事件 mNewAppTitle.setOnLeftButtonClickListener(new MyAppTitle.OnLeftButtonClickListener() { @Override public void onLeftButtonClick(View v) { Toast.makeText(MainActivity.this, "左左左左左", Toast.LENGTH_SHORT).show(); } }); mNewAppTitle.setOnRightButtonClickListener(new MyAppTitle.OnRightButtonClickListener() { @Override public void OnRightButtonClick(View v) { Toast.makeText(MainActivity.this, "右右右右右", Toast.LENGTH_SHORT).show(); } }); } }