how to set custom title bar TextView Value dynamically in android?

前端 未结 5 2005
离开以前
离开以前 2020-12-05 03:20

friends,

i have created custom title bar using following titlebar.xml file with code

 


        
相关标签:
5条回答
  • 2020-12-05 03:51

    Have you tried the setTitle() method of your Activity?

    0 讨论(0)
  • 2020-12-05 03:52

    my_title.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout android:id="@+id/header"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content" android:layout_width="fill_parent"
        android:background="#d4e9a9">
    
        <ImageView android:src="@drawable/jetpack"
            android:layout_width="wrap_content" android:layout_alignParentLeft="true"
            android:layout_centerVertical="true" android:id="@+id/back"
            android:layout_height="wrap_content" android:layout_alignParentTop="true" />
    
        <TextView android:id="@+id/title" android:layout_width="wrap_content"
            android:gravity="center_vertical" android:textSize="20px"
            android:textColor="#ffffff" android:layout_alignParentRight="true"
            android:text="New Title" android:background="#a5c639"
            android:layout_height="wrap_content" android:layout_alignParentTop="true"
            android:padding="9dip" android:layout_margin="5dip" />
    </RelativeLayout>
    

    Code:

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);      
    setContentView(R.layout.main);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);      
    
    ((TextView)findViewById(R.id.title)).setText("gradient shadow");
    
    findViewById(R.id.back).setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            ((TextView)findViewById(R.id.title)).setText("loce");
        }
    });
    

    Because custom title default is fixed you should write yourself a theme:

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/MyTheme">
    
    0 讨论(0)
  • 2020-12-05 03:54

    Try the following:

    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);        
         requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
         setContentView(R.layout.main);
         getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
         TextView mytitletext = (TextView) findViewById(R.id.myTitle);
         mytitletext.setText("========= NEW TITLE ==========");
    }
    

    You can use the hierarchyviewer to see if your view is accessible (you will see its id in the hierarchy graph)

    0 讨论(0)
  • 2020-12-05 03:55

    This is the way to set the custom title:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    
        setContentView(R.layout.main);
    
        if ( customTitleSupported ) {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
        }
    
        final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
        if ( myTitleText != null ) {
            myTitleText.setText("========= NEW TITLE ==========");
            myTitleText.setBackgroundColor(Color.GREEN);
        }
    
    
    }
    
    0 讨论(0)
  • 2020-12-05 04:05

    SDK 2.1+

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.main);
        setTitle("========= NEW TITLE ==========");
    }
    
    0 讨论(0)
提交回复
热议问题