我已经按照官方的HelloTabWidget Android教程进行操作,它完美无缺 . 接下来,我删除了标签图标,它仍然完美无缺 . 现在我想在每个标签中水平和垂直地将tabtext集中在一起,但是我无能为力 . 我试过搜索但找不到解决方案 .
我已经尝试将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);
}
这是main.xml文件:
<?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>
这就是我的标签现在的样子:
1 回答
如果您运行的是Android API Level> = 4,则可以使用TabHost.TabSpec.setIndicator(View view) . 这允许您指定任何视图,因此只需提供一个vertically center your text .
但是,如果您运行的API级别较低,请尝试使用this other stackoverflow question中的答案 . 它建议使用反射来设置视图,即使API没有发布所需的setIndicator方法 .