SoFunction
Updated on 2025-03-11

Android programming custom title bar (title bar) example

This article describes the method of programming custom title bar (title bar) on Android. Share it for your reference, as follows:

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class Test extends Activity {
  /** Called when the activity is first created. */
 boolean customTitleSupported;
  public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    //check if custom title is supported BEFORE setting the content view!
    customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView();
   //set custom titlebar
    customTitleBar(getText(.app_name).toString(), "hello world!!");
  }
 public void customTitleBar(String left, String right) {
 if (() > 20)
  right = (0, 20);
 // set up custom title
 if (customTitleSupported) {
  getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
   );
  TextView titleTvLeft = (TextView) findViewById();
  TextView titleTvRight = (TextView) findViewById();
  (left);
  (right);
  ProgressBar titleProgressBar;
  titleProgressBar = (ProgressBar) findViewById();
  // hide the progress bar if it is not needed
  ();
 }
 }
}

Layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:
 android:layout_width="fill_parent" android:layout_height="fill_parent"
 xmlns:andro>
 <TextView android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:
 android:text="left"></TextView>
 <TextView android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:
 android:text="right" android:layout_alignParentRight="true"></TextView>
 <ProgressBar android:
 style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:layout_centerVertical="true"
 android:layout_toRightOf="@+id/titleTvLeft" android:paddingLeft="3dip"></ProgressBar>
</RelativeLayout>

For more information about Android related content, please check out the topic of this site:Android View View Tips Summary》、《Android layout layout tips summary》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Android multimedia operation skills summary (audio, video, recording, etc.)》、《Summary of the usage of basic Android components"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.