我要更改选定标签的高度,tabwidget
如下图所示。
我使用的方法是:我使用了两个图像,第一个图像在顶部具有透明部分,第二个图像是完整图像,在选项卡上选择了第二个图像。但是这种方法并不适用于所有设备。我们必须创建具有相等高度的图像,并且还要将每个图像转换为九个补丁。
该图显示了我真正需要的。
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>
我不知道这是否行得通,但是您可以尝试保持已实现的方法onTabChangedListener
,TabHost
并为此实现,并在选择选项卡时在此处调整布局的大小。我没有尝试过,但是我的想法应该是这样的:
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)));
}
});
但是,如果此方法有效,则会发现另一个问题:在选择此选项卡之前,您不知道选择了哪个选项卡。您需要将每个选项卡选择保存到一个类变量中,以在选择新选项卡后将其大小调整为默认高度之前知道选择了哪个选项卡。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句