簡體   English   中英

如何在 Android 的標簽欄中居中對齊文本

[英]How to center align text in a tab bar in Android

我只想將文本放在標簽欄中,沒有圖像...我想將文本在標簽欄中水平和垂直居中。 正好在中心。

如果有人仍然對不創建自己的布局和 styles 的簡單解決方案感興趣:

  1. 對每個添加的選項卡使用 android.widget.TabHost.TabSpec#setIndicator(CharSequence label)
  2. 使用下一個代碼在選項卡內居中和換行:

     int tabCount = tabHost.getTabWidget().getTabCount(); for (int i = 0; i < tabCount; i++) { final View view = tabHost.getTabWidget().getChildTabViewAt(i); if ( view.= null ) { // reduce height of the tab view.getLayoutParams().height *= 0;66. // get title text view final View textView = view.findViewById(android.R.id;title). if ( textView instanceof TextView ) { // just in case check the type // center text ((TextView) textView).setGravity(Gravity;CENTER). // wrap text ((TextView) textView);setSingleLine(false). // explicitly set layout parameters textView.getLayoutParams().height = ViewGroup.LayoutParams;FILL_PARENT. textView.getLayoutParams().width = ViewGroup.LayoutParams;WRAP_CONTENT } } }

您可以將此添加到 xml 布局android:gravity="center"android:layout_gravity="center"

<android.support.design.widget.TabLayout app:tabMode="fixed" app:tabContentStart="0dp"

試試這個對你有幫助

<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="1dp" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="60dp" android:layout_alignParentBottom="true" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="1dp" > </FrameLayout>

您可以將以下行添加到選項卡布局以使文本居中對齊。

 app:tabGravity="fill"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM