Ich versuche, den folgenden Code zu verwenden, um den Text in der ActionBar
zu zentrieren, aber er richtet sich nach links aus.
Wie lässt du es in der Mitte erscheinen?
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle("Canteen Home");
actionBar.setHomeButtonEnabled(true);
actionBar.setIcon(R.drawable.back);
Um einen zentrierten Titel in ABS zu haben (wenn Sie dies in der Standardvariable ActionBar
haben möchten, entfernen Sie einfach die "Unterstützung" in den Methodennamen), können Sie Folgendes tun:
In Ihrer Aktivität in Ihrer onCreate()
-Methode:
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.abs_layout);
abs_layout
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:orientation="vertical">
<Android.support.v7.widget.AppCompatTextView
Android:id="@+id/tvTitle"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_gravity="center"
Android:gravity="center"
Android:textColor="#FFFFFF" />
</LinearLayout>
Jetzt sollten Sie eine Actionbar
mit nur einem Titel haben. Wenn Sie einen benutzerdefinierten Hintergrund festlegen möchten, legen Sie ihn im obigen Layout fest (vergessen Sie jedoch nicht, Android:layout_height="match_parent"
einzustellen).
oder mit:
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.yourimage));
Ich hatte mit den anderen Antworten nicht viel Erfolg ... Unten ist genau das, was auf Android 4.4.3 mit der ActionBar in der Support-Bibliothek v7 funktioniert. Ich habe es eingerichtet, um das Symbol der Navigationsleiste anzuzeigen ("Burger-Menü-Schaltfläche").
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:orientation="horizontal" >
<TextView
Android:id="@+id/actionbar_textview"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_gravity="center"
Android:maxLines="1"
Android:clickable="false"
Android:focusable="false"
Android:longClickable="false"
Android:textStyle="bold"
Android:textSize="18sp"
Android:textColor="#FFFFFF" />
</LinearLayout>
Java
//Customize the ActionBar
final ActionBar abar = getSupportActionBar();
abar.setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_background));//line under the action bar
View viewActionBar = getLayoutInflater().inflate(R.layout.actionbar_titletext_layout, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(//Center the textview in the ActionBar !
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.MATCH_PARENT,
Gravity.CENTER);
TextView textviewTitle = (TextView) viewActionBar.findViewById(R.id.actionbar_textview);
textviewTitle.setText("Test");
abar.setCustomView(viewActionBar, params);
abar.setDisplayShowCustomEnabled(true);
abar.setDisplayShowTitleEnabled(false);
abar.setDisplayHomeAsUpEnabled(true);
abar.setIcon(R.color.transparent);
abar.setHomeButtonEnabled(true);
Definieren Sie Ihre eigene benutzerdefinierte Ansicht mit Titeltext, und übergeben Sie dann LayoutParams an setCustomView (), wie Sergii sagt.
ActionBar actionBar = getSupportActionBar()
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(getLayoutInflater().inflate(R.layout.action_bar_home, null),
new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.MATCH_PARENT,
Gravity.CENTER
)
);
BEARBEITET: Zumindest für die Breite sollten Sie WRAP_CONTENT oder Ihre Navigationsleiste, das App-Symbol usw. verwenden. Wird nicht angezeigt (benutzerdefinierte Ansicht wird über anderen Ansichten in der Aktionsleiste angezeigt). Dies tritt insbesondere dann auf, wenn keine Aktionsschaltfläche angezeigt wird.
BEARBEITET: Entspricht im XML-Layout:
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="wrap_content"
Android:layout_height="match_parent"
Android:layout_gravity="center_horizontal"
Android:orientation="vertical">
Dies erfordert nicht, dass LayoutParams angegeben wird.
actionBar.setCustomView(getLayoutInflater().inflate(R.layout.action_bar_home, null);
Nur eine kurze Ergänzung zu Ahmads Antwort. Sie können getSupportActionBar (). SetTitle nicht mehr verwenden, wenn Sie eine benutzerdefinierte Ansicht mit einer TextView verwenden. Um also den Titel festzulegen, wenn Sie mehrere Aktivitäten mit dieser benutzerdefinierten ActionBar (mit dieser einen xml) haben, weisen Sie in Ihrer onCreate () - Methode after eine benutzerdefinierte Ansicht zu:
TextView textViewTitle = (TextView) findViewById(R.id.mytext);
textViewTitle.setText(R.string.title_for_this_activity);
OK. Nach langem Nachforschen, kombiniert mit der akzeptierten Antwort oben, habe ich eine Lösung gefunden, die auch funktioniert, wenn Sie andere Elemente in Ihrer Aktionsleiste haben (Zurück/Home-Button, Menü-Button). Grundsätzlich habe ich die Override-Methoden in eine grundlegende Aktivität (die alle anderen Aktivitäten ausdehnen) eingefügt und den Code dort platziert. Dieser Code legt den Titel jeder Aktivität fest, wie er in AndroidManifest.xml bereitgestellt wird, und führt auch andere benutzerdefinierte Elemente aus (z. B. das Festlegen eines benutzerdefinierten Farbtons für die Aktionsleistenschaltflächen und eine benutzerdefinierte Schriftart für den Titel). Sie müssen nur die Schwerkraft in action_bar.xml weglassen und stattdessen Padding verwenden. actionBar != null
check wird verwendet, da nicht alle meine Aktivitäten eine haben.
Getestet am 4.4.2 und 5.0.1
public class BaseActivity extends AppCompatActivity {
private ActionBar actionBar;
private TextView actionBarTitle;
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
super.onCreate(savedInstanceState);
...
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setElevation(0);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.action_bar);
LinearLayout layout = (LinearLayout) actionBar.getCustomView();
actionBarTitle = (TextView) layout.getChildAt(0);
actionBarTitle.setText(this.getTitle());
actionBarTitle.setTypeface(Utility.getSecondaryFont(this));
toolbar = (Toolbar) layout.getParent();
toolbar.setContentInsetsAbsolute(0, 0);
if (this.getClass() == BackButtonActivity.class || this.getClass() == AnotherBackButtonActivity.class) {
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
Drawable wrapDrawable = DrawableCompat.wrap(getResources().getDrawable(R.drawable.ic_back));
DrawableCompat.setTint(wrapDrawable, getResources().getColor(Android.R.color.white));
actionBar.setHomeAsUpIndicator(wrapDrawable);
actionBar.setIcon(null);
}
else {
actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setHomeAsUpIndicator(null);
actionBar.setIcon(null);
}
}
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception ex) {
// Ignore
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (actionBar != null) {
int padding = (getDisplayWidth() - actionBarTitle.getWidth())/2;
MenuInflater inflater = getMenuInflater();
if (this.getClass() == MenuActivity.class) {
inflater.inflate(R.menu.activity_close_menu, menu);
}
else {
inflater.inflate(R.menu.activity_open_menu, menu);
}
MenuItem item = menu.findItem(R.id.main_menu);
Drawable icon = item.getIcon();
icon.mutate().mutate().setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_IN);
item.setIcon(icon);
ImageButton imageButton;
for (int i =0; i < toolbar.getChildCount(); i++) {
if (toolbar.getChildAt(i).getClass() == ImageButton.class) {
imageButton = (ImageButton) toolbar.getChildAt(i);
padding -= imageButton.getWidth();
break;
}
}
actionBarTitle.setPadding(padding, 0, 0, 0);
}
return super.onCreateOptionsMenu(menu);
} ...
Und meine action_bar.xml sieht folgendermaßen aus (wenn jemand interessiert ist):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:layout_gravity="center"
Android:orientation="horizontal">
<TextView
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:textColor="@color/actionbar_text_color"
Android:textAllCaps="true"
Android:textSize="9pt"
/>
</LinearLayout>
EDIT: Wenn Sie nach dem Laden der Aktivität den Titel in etwas anderes ändern müssen (onCreateOptionsMenu wurde bereits aufgerufen), fügen Sie ein anderes TextView in Ihre action_bar.xml ein und verwenden Sie den folgenden Code, um dieses neue Element "aufzufüllen" TextView, setze den Text und zeige ihn:
protected void setSubTitle(CharSequence title) {
if (!initActionBarTitle()) return;
if (actionBarSubTitle != null) {
if (title != null || title.length() > 0) {
actionBarSubTitle.setText(title);
setActionBarSubTitlePadding();
}
}
}
private void setActionBarSubTitlePadding() {
if (actionBarSubTitlePaddingSet) return;
ViewTreeObserver vto = layout.getViewTreeObserver();
if(vto.isAlive()){
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int padding = (getDisplayWidth() - actionBarSubTitle.getWidth())/2;
ImageButton imageButton;
for (int i = 0; i < toolbar.getChildCount(); i++) {
if (toolbar.getChildAt(i).getClass() == ImageButton.class) {
imageButton = (ImageButton) toolbar.getChildAt(i);
padding -= imageButton.getWidth();
break;
}
}
actionBarSubTitle.setPadding(padding, 0, 0, 0);
actionBarSubTitlePaddingSet = true;
ViewTreeObserver obs = layout.getViewTreeObserver();
obs.removeOnGlobalLayoutListener(this);
}
});
}
}
protected void hideActionBarTitle() {
if (!initActionBarTitle()) return;
actionBarTitle.setVisibility(View.GONE);
if (actionBarSubTitle != null) {
actionBarSubTitle.setVisibility(View.VISIBLE);
}
}
protected void showActionBarTitle() {
if (!initActionBarTitle()) return;
actionBarTitle.setVisibility(View.VISIBLE);
if (actionBarSubTitle != null) {
actionBarSubTitle.setVisibility(View.GONE);
}
}
EDIT (25.08.2016): Dies funktioniert nicht mit der Revision von appcompat 24.2.0 (August 2016), wenn Ihre Aktivität einen "Zurück-Button" hat. Ich habe einen Fehlerbericht eingereicht ( Ausgabe 220899 ), weiß aber nicht, ob er von Nutzen ist (Zweifel werden in Kürze behoben). In der Zwischenzeit besteht die Lösung darin, zu prüfen, ob die Klasse des Kindes AppCompatImageButton.class entspricht, und dies zu tun, die Breite nur um 30% zu erhöhen (z. B. appCompatImageButton.getWidth () * 1.3, bevor dieser Wert von der ursprünglichen Auffüllung abgezogen wird):
padding -= appCompatImageButton.getWidth()*1.3;
In der Zwischenzeit habe ich dort einige Polster-/Randüberprüfungen eingefügt:
Class<?> c;
ImageButton imageButton;
AppCompatImageButton appCompatImageButton;
for (int i = 0; i < toolbar.getChildCount(); i++) {
c = toolbar.getChildAt(i).getClass();
if (c == AppCompatImageButton.class) {
appCompatImageButton = (AppCompatImageButton) toolbar.getChildAt(i);
padding -= appCompatImageButton.getWidth()*1.3;
padding -= appCompatImageButton.getPaddingLeft();
padding -= appCompatImageButton.getPaddingRight();
if (appCompatImageButton.getLayoutParams().getClass() == LinearLayout.LayoutParams.class) {
padding -= ((LinearLayout.LayoutParams) appCompatImageButton.getLayoutParams()).getMarginEnd();
padding -= ((LinearLayout.LayoutParams) appCompatImageButton.getLayoutParams()).getMarginStart();
}
break;
}
else if (c == ImageButton.class) {
imageButton = (ImageButton) toolbar.getChildAt(i);
padding -= imageButton.getWidth();
padding -= imageButton.getPaddingLeft();
padding -= imageButton.getPaddingRight();
if (imageButton.getLayoutParams().getClass() == LinearLayout.LayoutParams.class) {
padding -= ((LinearLayout.LayoutParams) imageButton.getLayoutParams()).getMarginEnd();
padding -= ((LinearLayout.LayoutParams) imageButton.getLayoutParams()).getMarginStart();
}
break;
}
}
ohne Customview ist es in der Lage, den Actionbar-Titel zu zentrieren
int titleId = getResources().getIdentifier("action_bar_title", "id", "Android");
TextView abTitle = (TextView) findViewById(titleId);
abTitle.setTextColor(getResources().getColor(R.color.white));
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
abTitle.setGravity(Gravity.CENTER);
abTitle.setWidth(metrics.widthPixels);
getActionBar().setTitle("I am center now");
Glückliche Kodierung Danke dir.
Es funktioniert gut.
activity = (AppCompatActivity) getActivity();
activity.getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.custom_actionbar, null);
ActionBar.LayoutParams p = new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
Gravity.CENTER);
((TextView) v.findViewById(R.id.title)).setText(FRAGMENT_TITLE);
activity.getSupportActionBar().setCustomView(v, p);
activity.getSupportActionBar().setDisplayShowTitleEnabled(true);
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Unterhalb des Layouts von custom_actionbar:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="match_parent"
Android:layout_height="match_parent">
<TextView
Android:id="@+id/title"
Android:layout_width="wrap_content"
Android:text="Example"
Android:layout_height="wrap_content"
Android:layout_centerInParent="true"
Android:ellipsize="end"
Android:maxLines="1"
Android:textAppearance="?android:attr/textAppearanceMedium"
Android:textColor="@color/colorBlack" />
</RelativeLayout>
Nach langer Recherche: Das funktioniert eigentlich:
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getActionBar().setCustomView(R.layout.custom_actionbar);
ActionBar.LayoutParams p = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
p.gravity = Gravity.CENTER;
Sie müssen das Layout custom_actionbar.xml definieren, das Ihren Anforderungen entspricht, z. :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="match_parent"
Android:layout_height="50dp"
Android:background="#2e2e2e"
Android:orientation="vertical"
Android:gravity="center"
Android:layout_gravity="center">
<ImageView
Android:id="@+id/imageView1"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:src="@drawable/top_banner"
Android:layout_gravity="center"
/>
</LinearLayout>
Beste und einfachste Möglichkeit, speziell für diejenigen, die nur Textansicht mit Schwerpunkt ohne XML-Layout wünschen.
AppCompatTextView mTitleTextView = new AppCompatTextView(getApplicationContext());
mTitleTextView.setSingleLine();
ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
actionBar.setCustomView(mTitleTextView, layoutParams);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP);
mTitleTextView.setText(text);
mTitleTextView.setTextAppearance(getApplicationContext(), Android.R.style.TextAppearance_Medium);
Sie müssen ActionBar.LayoutParams.WRAP_CONTENT
und ActionBar.DISPLAY_HOME_AS_UP
einstellen.
View customView = LayoutInflater.from(this).inflate(R.layout.actionbar_title, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP );
Für Kotlin-Benutzer:
Verwenden Sie den folgenden Code in Ihrer Aktivität:
// Set custom action bar
supportActionBar?.displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM
supportActionBar?.setCustomView(R.layout.action_bar)
// Set title for action bar
val title = findViewById<TextView>(R.id.titleTextView)
title.setText(resources.getText(R.string.app_name))
Und das XML/Ressourcen-Layout:
<?xml version="1.0" encoding="utf-8"?>
<Android.support.constraint.ConstraintLayout
xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:app="http://schemas.Android.com/apk/res-auto"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="match_parent">
<TextView
Android:id="@+id/titleTextView"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text="Title"
Android:textColor="@color/black"
Android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</Android.support.constraint.ConstraintLayout>
Code hier arbeitet für mich.
// Activity
public void setTitle(String title){
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
TextView textView = new TextView(this);
textView.setText(title);
textView.setTextSize(20);
textView.setTypeface(null, Typeface.BOLD);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
textView.setGravity(Gravity.CENTER);
textView.setTextColor(getResources().getColor(R.color.white));
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(textView);
}
// Fragment
public void setTitle(String title){
((AppCompatActivity)getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
TextView textView = new TextView(getActivity());
textView.setText(title);
textView.setTextSize(20);
textView.setTypeface(null, Typeface.BOLD);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
textView.setGravity(Gravity.CENTER);
textView.setTextColor(getResources().getColor(R.color.white));
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
((AppCompatActivity)getActivity()).getSupportActionBar().setCustomView(textView);
}
Die anderen Lernprogramme, die ich gesehen habe, überschreiben das gesamte Aktionsleistenlayout, das die Menüelemente verdeckt. Ich habe es mit den folgenden Schritten gemacht:
Erstellen Sie eine XML-Datei wie folgt:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="match_parent"
Android:layout_height="match_parent" >
<TextView
Android:id="@+id/title"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_centerInParent="true"
Android:ellipsize="end"
Android:maxLines="1"
Android:text="@string/app_name"
Android:textAppearance="?android:attr/textAppearanceMedium"
Android:textColor="@Android:color/white" />
</RelativeLayout>
Und in der Klasse:
LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.action_bar_title, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
TextView titleTV = (TextView) v.findViewById(R.id.title);
titleTV.setText("Test");
Eine reine Kotlin-Lösung, für die keine Änderungen in den XML-Layouts erforderlich sind:
//Function to call in onResume() of your activity
private fun centerToolbarText() {
val mTitleTextView = AppCompatTextView(this)
mTitleTextView.text = title
mTitleTextView.setSingleLine()//Remove it if you want to allow multiple lines in the toolbar
mTitleTextView.textSize = 25f
val layoutParams = Android.support.v7.app.ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT
)
layoutParams.gravity = Gravity.CENTER
supportActionBar?.setCustomView(mTitleTextView,layoutParams)
supportActionBar?.displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM
}
Dieser Code wird nicht ausgeblendet, sondern der Titel wird in der Mitte ausgerichtet.
rufen Sie diese Methode in oncreate auf
centerActionBarTitle();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
myActionBar.setIcon(new ColorDrawable(Color.TRANSPARENT));
private void centerActionBarTitle() {
int titleId = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
titleId = getResources().getIdentifier("action_bar_title", "id", "Android");
} else {
// This is the id is from your app's generated R class when
// ActionBarActivity is used for SupportActionBar
titleId = R.id.action_bar_title;
}
// Final check for non-zero invalid id
if (titleId > 0) {
TextView titleTextView = (TextView) findViewById(titleId);
DisplayMetrics metrics = getResources().getDisplayMetrics();
// Fetch layout parameters of titleTextView
// (LinearLayout.LayoutParams : Info from HierarchyViewer)
LinearLayout.LayoutParams txvPars = (LayoutParams) titleTextView.getLayoutParams();
txvPars.gravity = Gravity.CENTER_HORIZONTAL;
txvPars.width = metrics.widthPixels;
titleTextView.setLayoutParams(txvPars);
titleTextView.setGravity(Gravity.CENTER);
}
}