我遵循了官方的HelloTabWidget Android教程,它可以完美运行。接下来,我删除了选项卡图标,它仍然可以正常运行。现在,我想在每个标签中将标签文本水平和垂直居中,但我一无所知。我尝试搜索,但是找不到解决方案。
我尝试将android:layout_gravity添加到我的main.xml文件中,但没有帮助。
这是我的onCreate main.java文件:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TabHost tabHost = getTabHost(); // The activity TabHost TabHost.TabSpec spec; // Resusable TabSpec for each tab Intent intent; // Reusable Intent for each tab intent = new Intent().setClass(this, Indkobsliste.class); spec = tabHost.newTabSpec("tab1").setIndicator("Tab1") .setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, Opskrifter.class); spec = tabHost.newTabSpec("tab2").setIndicator("Tab2") .setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, Madplan.class); spec = tabHost.newTabSpec("tab3").setIndicator("Tab3") .setContent(intent); tabHost.addTab(spec); tabHost.setCurrentTab(1); }
This is the main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"/>
</LinearLayout>
</TabHost>
这是我的标签现在的外观:
最佳答案
如果您正在运行Android API Level> = 4,则可以使用TabHost.TabSpec.setIndicator(View view)。这允许您指定任何视图,因此只需在您的vertically center your text位置提供一个即可。
但是,如果您正在运行较低的API级别,请尝试使用this other stackoverflow question中的答案。它建议即使API不发布所需的setIndicator方法,也要使用反射来设置视图。