SoFunction
Updated on 2025-04-07

Android Custom Universal TitleBar

This example shares the specific code of Android custom universal title bar for your reference. The specific content is as follows/p>

1Customize a public_titlebar.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
 android:
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
 <ImageView
  android:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@drawable/z"/>
 <LinearLayout
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:layout_weight="1">
  <TextView
   android:
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:text="tvTitle"/>
  <TextView
   android:
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:text="tvRight"/>
 </LinearLayout>
</LinearLayout>

2. Create a new one in the values ​​folder

<?xml version="1.0" encoding="utf-8"?>
 <resources>
 <declare-styleable name="CustomerTitleBar">
  <attr name="left_image" format="reference"></attr>
  <attr name="center_text" format="string"></attr>
  <attr name="center_text_color" format="color"></attr>
  <attr name="center_text_size" format="dimension"></attr>
 </declare-styleable>
 </resources>

3. Customize CustomerTitleBar class inherits LinearLayout, unify the page title bar, and include interface callbacks in the project

public class CustomerTitleBar extends LinearLayout {
private LinearLayout rootView;
private ImageView ivLeft;
private TextView tvTitle;
private TextView tvRight;
//3. Declare the callback objectprivate CustomerClick leftClick;

/**
 * @param context
 */
//When you directly new a Custom View instance in the code, the first constructor will be calledpublic CustomerTitleBar(Context context) {
 this(context,null);
}

//When calling Custom View in the xml layout file, the second constructor will be calledpublic CustomerTitleBar(Context context,AttributeSet attrs) {
 this(context, attrs,-1);
}

//When you call Custom View in the xml layout file and there are custom properties in the Custom View tag, the second constructor is called here.public CustomerTitleBar(Context context,AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 init(context);
 initAttrs(context,attrs);
 initLister();
}

private void init(Context context) {
 rootView= (LinearLayout) (context,.layout_customer_title_bar,this);
 ivLeft=();
 tvTitle=();
 tvRight=();
}

private void initAttrs(Context context, AttributeSet attrs) {
 TypedArray typedArray=(attrs,);
 Drawable drawable=(.CustomerTitleBar_left_image);
 if (drawable!=null){
  (drawable);
 }
 CharSequence text = (.CustomerTitleBar_center_text);
 if (!(text)) {
  (text);
 }
 int color = (.CustomerTitleBar_center_text_color, -1);

 if (color != -1) {
  (color);
 }

 float dimension = (.CustomerTitleBar_center_text_size, 0f);

 (dimension);
}

private void initLister() {
 (new OnClickListener() {
  @Override
  public void onClick(View v) {
   //5. Use interface callback   if (leftClick!=null){
    (v);
   }
  }
 });
}
//4. Provide a set method for callback objectpublic void setLeftClick(CustomerClick leftClick) {
  = leftClick;
}
//1. Define the callback interfaceinterface CustomerClick{
 void onLefClick(View view);
}
}

4. References in layout files

&lt;
 android:
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:gravity="center"
 customer:center_text="Bubble"
 customer:center_text_color="#ff00ff"
 customer:center_text_size="20sp" /&gt;

5. Usage in Activity

public class MainActivity extends AppCompatActivity {
 private CustomerTitleBar ctTitle;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  setContentView(.activity_main);
  ctTitle=findViewById();
  //6. Use  (new () {
   @Override
   public void onLefClick(View view) {
    (,"Bubble",Toast.LENGTH_LONG).show();
   }
  });
}
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.