Frage

Freunde,

i benutzerdefinierte Titelleiste mit folgenden titlebar.xml Datei mit dem Code erstellt habe

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:id="@+id/myTitle" 
  android:text="This is my new title" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:textColor="@color/titletextcolor"
  android:layout_marginLeft="25px"
   android:paddingTop="3px" 
   /> 

und Java-Code auf jeder Aktivität benutzerdefinierte Titelleiste angezeigt werden soll.

@Override
    public void onCreate(Bundle savedInstanceState)
    {
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);

super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


}

Nun will ich Textview Wert dynamisch in jeder Aktivität einstellen kann mir eine Führung, wie ich dies erreichen kann?

mit findViewById hier i-Fundstelle dieser Textview dont get Wert zu setzen, weil Haupt Layout enthält keine Textbox mit einem solchen Namen, aber mytitle.

würde jede Hilfe appriciated werden.

War es hilfreich?

Lösung

Dies ist der Weg, um den benutzerdefinierten Titel zu setzen:

@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);
    }


}

Andere Tipps

SDK 2.1 +

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    setContentView(R.layout.main);
    setTitle("========= NEW TITLE ==========");
}

Haben Sie versucht, die setTitle() Methode Ihrer Activity?

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");
    }
});

Da benutzerdefinierten Titel Standard festgelegt ist, sollten Sie sich ein Thema schreiben:

<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/MyTheme">

Versuchen Sie Folgendes:

@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 ==========");
}

Sie haben die hierarchyviewer verwenden können, um zu sehen, ob Ihre Ansicht zugänglich ist (Sie seine ID in der Hierarchie Diagramm sehen)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top