[英]How to make android action bar title center aligned?
我嘗試了自己的自定義操作欄,讓操作欄標題位於中間位置。 有效。 但是,在我添加操作欄中的選項菜單后,標題再次向左移動。 我應該怎么做才能讓標題集中在動作欄的中間? 我的代碼是:
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
View cView = getLayoutInflater().inflate(R.layout.custom_actionbar, null);
actionBar.setCustomView(cView);
我的布局是
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/holo_blue_light"
android:orientation="vertical" >
<TextView
android:id="@+id/apptitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal|center_vertical"
android:gravity="center_horizontal|center_vertical"
android:text="@string/app_name" />
我相信,您使用以下設置自定義主題。
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.abs_layout);
如果沒有,那么請添加第一行。 自定義布局中的文本居中。 (重力=中心)
試試這個。
我沒有顯示活動的標題,而是按照您的建議使用customView,從而完成了我想要的任務。 我錯過了一段時間的關鍵是你必須使用setCustomView(View,ActionVar.LayoutParams)方法才能使自定義視圖居中,只需在自定義視圖布局文件中設置layout_gravity =“center”就行不通了。 這是一個簡短的代碼片段,展示了我如何使用它:
TextView customView = (TextView)
LayoutInflater.from(this).inflate(R.layout.actionbar_custom_title_view_centered,
null);
ActionBar.LayoutParams params = new
ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
customView.setText("Some centered text");
getSupportActionBar().setCustomView(customView, params);
這種方法適用於至少3.2,2.3和2.2設備。
來源https://groups.google.com/forum/#!msg/actionbarsherlock/A9Ponma6KKI/Llsp41jWLEgJ
首先,創建一個.xml文件,如下所示:(action_bar.xml)
<?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="horizontal">
<TextView
android:id="@+id/actionbar_title"
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="#000000" />
</LinearLayout>
然后,在您的活動中:
View view = getLayoutInflater().inflate(R.layout.action_bar, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.MATCH_PARENT,
Gravity.CENTER);
TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
Title.setText("Your Title");
getSupportActionBar().setCustomView(view,params);
getSupportActionBar().setDisplayShowCustomEnabled(true); //show custom title
getSupportActionBar().setDisplayShowTitleEnabled(false); //hide the default title
我已經嘗試了很多解決方案,只有這個有效。 希望它能幫到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.