[英]Center App Bar title on Android
我正在嘗試使用居中對齊的標題創建自己的自定義應用欄。 Android Studio的編輯器顯示我的標題正確居中,但不在我的實際設備上。
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.encounter_toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.encounter_menu_bar_layout);
}
meet_menu_bar_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:id="@+id/encounter_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/untitled"
android:textSize="20sp"
android:textColor="@color/titleTextColor"
android:textStyle="bold"
android:layout_gravity="center" />
</LinearLayout>
meet_menu_bar.xml中沒有任何內容:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
</menu>
編輯
許多人建議我將android:layout_gravity
更改為android:gravity
。 我也嘗試過,但仍然無法正確排列。
我正在啟用應用程序欄以顯示在特定片段上,如下所示:
EncounterFragment.java
public class EncounterFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
return inflater.inflate(R.layout.fragment_encounter, container, false);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
menuInflater.inflate(R.menu.encounter_menu_bar, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO: This
return super.onOptionsItemSelected(item);
}
}
我也忘了提及我將菜單附加到一個片段中。 這是它的布局:
fragment_encounter.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/encounter_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</LinearLayout>
嘗試添加android:gravity="center"
。 發生這種情況是因為您必須使整個文本居中,而不僅僅是布局的中心
使用android:layout_gravity="center"
例:
<LinearLayout 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"
android:orientation="vertical">
<Button
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
注意: layout_gravity
, 不是 gravity
在您的情況下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:id="@+id/encounter_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/untitled"
android:textSize="20sp"
android:textColor="@color/titleTextColor"
android:textStyle="bold"
android:layout_gravity="center" />
</LinearLayout>
將textview更改為此
使它與父項的寬度匹配,並使重心位於文本的中心
<TextView
android:id="@+id/encounter_name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/untitled"
android:textSize="20sp"
android:textColor="@color/titleTextColor"
android:textStyle="bold"
android:gravity="center" />
您想要將xml視圖更改為此:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
#this should be horizontal, not vertical.
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:id="@+id/encounter_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/untitled"
android:textSize="20sp"
android:textColor="@color/titleTextColor"
android:textStyle="bold"
/>
首先,您需要使線性布局水平而不是垂直。 通過使其垂直,您將其向左對齊。
然后,您需要確保textview
寬度和高度與父項匹配,而不是僅包裝內容。 然后,您可以使用gravity
屬性來確保文本居中。 以前使用重力時, textview
只是包裝內容,因此它不在textview中居中。 通過匹配父視圖范圍,您將能夠根據需要居中文本。
這就是您的工具欄XML的樣子
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="@android:color/white"
android:background="?attr/colorPrimary">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toolbar Title"
android:textColor="@android:color/white"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_gravity="center"
/>
</android.support.v7.widget.Toolbar>
然后在您的活動中
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.