我想更改 tabwidget
中所选选项卡的高度,如下图所示。
我使用的方法:我使用了两张图片,第一张图片上面有透明部分,第二张是完整图片,在选择的选项卡上,我用第二张图片更改了第一张图片。但这种方法并不适用于所有设备。我们必须创建具有相同高度的图像,并将每个图像转换为 9-patch 。
图中显示了我所需要的。
TabHost.TabSpec spec = mTabHost.newTabSpec(AppConstants.TAB_A);
spec.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
return findViewById(R.id.realtabcontent);
}
});
spec.setIndicator(createTabView(R.drawable.hometabselection));//style applied here on each tab
mTabHost.addTab(spec);
spec = mTabHost.newTabSpec(AppConstants.TAB_B);
spec.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
return findViewById(R.id.realtabcontent);
}
});
spec.setIndicator(createTabView(R.drawable.realtabselection));
mTabHost.addTab(spec);
spec = mTabHost.newTabSpec(AppConstants.TAB_C);
spec.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
return findViewById(R.id.realtabcontent);
}
});
spec.setIndicator(createTabView(R.drawable.topnewwsselection));
mTabHost.addTab(spec);
spec = mTabHost.newTabSpec(AppConstants.TAB_D);
spec.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
return findViewById(R.id.realtabcontent);
}
});
spec.setIndicator(createTabView(R.drawable.treadingselection));
mTabHost.addTab(spec);
hometabselection.xml//在用于所有不同选项卡选择的drawable中
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_selected="true"
android:drawable="@drawable/homeselected" />//full image
<item
android:state_selected="false"
android:drawable="@drawable/homenormal" />//image with some portion transparent on top of that
</selector>
布局
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="@dimen/fr_layout_width"
android:layout_height="@dimen/fr_layout_height"
android:layout_weight="0" />
<FrameLayout
android:id="@+android:id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="@dimen/fr_layout_height"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="@dimen/hdpi_tab_layout_height"
android:layout_weight="0"
android:divider="@drawable/tab_bg"
android:background="@android:color/transparent"
android:dividerPadding="@dimen/tab_divider_padding"
android:orientation="horizontal" />
</LinearLayout>
</TabHost>
最佳答案
我不知道这是否行得通,但您可以尝试保留已实现的方法,并为该 TabHost
额外实现一个 onTabChangedListener
并调整布局大小当一个选项卡被选中时。我没试过,但我的想法应该是这样的:
your_tabhost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(final String tabId) {
final TabHost th = (TabHost) findViewById(android.R.id.tabhost));
final LinearLayout lLayout = (LinearLayout) th.getTabWidget().getChildTabViewAt(th.getCurrentTab());
ViewGroup.LayoutParams curParams = lLayout.getLayoutParams();
lLayout.setLayoutParams(new FrameLayout.LayoutParams(curParams.width, (int) (curParams.height * 1.20)));
}
});
但是,如果这可行,您会发现另一个问题:您不知道在选择它之前选择了哪个选项卡。您需要将每个选项卡选择保存到一个类变量中,以便在选择新选项卡后将其调整为默认高度之前了解选择了哪个选项卡。
关于Android更改tabwidget中所选标签的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21399570/