Ich habe eine XML-Datei, die ich mit so vielen Aktivitäten mit Fragmenten-Datei verwende, aber mein Problem ist, dass ich den gewünschten Text nicht in der Symbolleiste anzeigen kann. Ich verwende diese XML-Datei auf diese Weise, weil ich eine Navigationsleiste habe und etwas erledigen musste also musste ich es so machen.
mein xml:
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:id="@+id/frame_container"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
tools:context=".StatusActivity"
Android:orientation="vertical" >
<Android.support.v7.widget.Toolbar
Android:id="@+id/toolbar"
style="@style/ToolBarStyle"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:background="?attr/colorPrimary"
Android:minHeight="@dimen/abc_action_bar_default_height_material" />
</RelativeLayout>
Eine meiner Aktivitäten:
public class GroupChatActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Groups history");
Aniways.init(this);
if(savedInstanceState == null)
{
FragmentManager manager = getSupportFragmentManager();
Fragment fragment = GroupChatFragment.newInstance(getIntent().getIntExtra("uid", 0));
manager.beginTransaction().add(R.id.frame_container, fragment).commit();
}
}
}
wie Sie sehen, versuche ich, der Aktionsleiste einen Titel zuzuweisen, aber es funktioniert nicht.
getSupportActionBar().setDisplayShowTitleEnabled(true);
Rahmen,
app:title="@string/my_title"
in der Deklaration der Android.support.v7.widget.Toolbar wird der Titel in der Symbolleiste hart codiert.
Um den Titel programmgesteuert zu setzen,
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
toolbar.setTitle("my title");
setSupportActionBar(toolbar);
in deiner Aktivitätsklasse.
Probieren Sie das aus .. diese Methode funktioniert für mich .. !! hoffe es kann jemandem helfen .. !!
<Android.support.v7.widget.Toolbar
xmlns:app="http://schemas.Android.com/apk/res-auto"
Android:id="@+id/my_awesome_toolbar"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:background="?attr/colorPrimary"
Android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >
<TextView
Android:id="@+id/toolbar_title"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_gravity="center"
Android:singleLine="true"
Android:text="Toolbar Title"
Android:textColor="@Android:color/white"
Android:textSize="18sp"
Android:textStyle="bold" />
</Android.support.v7.widget.Toolbar>
EDIT
Sie können dies auch verwenden.
setSupportActionBar(toolbar);
if (getSupportActionBar() != null)
getSupportActionBar().setTitle("Toolbar title");
getActionBar().setTitle("Groups History");
oder wenn Sie AppCompat
Bibliotheken verwenden;
getSupportActionBar().setTitle("Groups History");
toolbar.setTitle('Groups history');
ausprobieren
Ich habe eine benutzerdefinierte Aktionsleiste erstellt.
Layout iguepardos_action_bar.xml mit diesem Code
<?xml version="1.0" encoding="utf-8"?>
<Android.support.v7.widget.Toolbar
xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:local="http://schemas.Android.com/apk/res-auto"
Android:id="@+id/toolbar"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:background="@color/blanco"
Android:minHeight="?attr/actionBarSize">
<TextView
Android:id="@+id/toolbar_title"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_gravity="left"
Android:singleLine="true"
Android:text="Toolbar Title"
Android:textColor="@color/naranja"
Android:textSize="18sp" />
</Android.support.v7.widget.Toolbar>
In meiner Klasse erweiterte AppCompatActivity hatte ich Folgendes:
protected void onCreate(Bundle savedInstanceState) {
....
....
getSupportActionBar().setDisplayShowCustomEnabled(true); // is for specify use custom bar
getSupportActionBar().setCustomView(R.layout.iguepardos_action_bar); // my custom layout
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // Button for come back
View mCustomView = getSupportActionBar().getCustomView(); // Set the view
TextView TitleToolBar = (TextView) mCustomView.findViewById(R.id.toolbar_title); // find title control
TitleToolBar.setText("The Title Show"); // Set the Title
}