我的主要 Activity 是 TabActivity
,但我的选项卡不需要图标。我知道我可以使用重载的 TabHost.TabSpec.setIndicator(CharSequence label)
方法省略图标,如 this question 中的回答.
但是图标所在的空间仍然是保留的,所以浪费了很多空间。这已在 this question 中注明.看来我不能只降低标签高度。我必须为我自己的 View
提供 TabHost.TabSpec.setIndicator(View view)
重载。
很好,但我想确保其余样式(背景/着色)在所有设备上始终保持“android”风格,就好像我使用了默认选项卡指示器一样。所以我不想提供我自己的颜色。
我找到了 @android:color/tab_indicator_text
并将它用于 TextView
的文本颜色似乎按预期工作。但我不知道从哪里获得默认的标签背景颜色。我还发现了 @android:style/Widget.TabWidget
样式,但是将它应用到 TextView
和周围的 LinearLayout
似乎帮助。
这是我的选项卡指示器 View xml 文件,取自一些教程:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="12dip" >
<TextView
android:id="@+id/tabText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/tab_indicator_text" />
</LinearLayout>
我如何获得“android 默认”选项卡样式?
编辑: 接下来,我尝试通过 setIndicator(CharSequence)
添加默认选项卡,获取其指示器 View 的 getBackground
可绘制对象,清除所有选项卡,添加我的自定义 View (见上文),然后将我检索到的可绘制对象放入 setBackground
以用于新指示器。
结果看起来有点对也有点错。首先,它们又太高了,所以我一无所获。另一方面,所有选项卡的背景状态现在都以某种方式链接( Activity 、突出显示、选中等)。在可绘制对象上使用 mutate()
根本没有帮助。下一个技巧:我添加了三个默认选项卡,取出它们的所有背景,清除它们,并为我自己的三个选项卡各设置一个背景。
嗯,这就解决了第二个问题。他们的状态不再关联。但他们仍然太高了。所以我使用 updateViewLayout
降低了 TabWidget
的高度。这可行,但会在选项卡指示器的顶部留下一个变色的条。
实际上,我最终遇到的情况与使用 TabHost.TabSpec.setIndicator(CharSequence label)
方法时相同。无论如何,所有这些技巧都让我感到肮脏。有没有一种优雅的方法可以在自定义 View 上获取默认的 android 样式?
最佳答案
此代码取自 android 源 tab_indicator.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="64dip"
android:layout_weight="1"
android:layout_marginLeft="-3dip"
android:layout_marginRight="-3dip"
android:orientation="vertical"
android:background="@android:drawable/tab_indicator">
<ImageView android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
style="?android:attr/tabWidgetStyle"
/>
</RelativeLayout>
你可以尝试只拿这段代码,删除ImageView
,将TextView
中的layout_centerHorizontal
改为layout_center
,降低 RelativeLayout
的高度,并在 TabHost.TabSpec.setIndicator(View view)
中提供此 View 。只有对 @android
资源的引用,因此您不需要创建任何可绘制对象或文本样式。
编辑
选项卡指示器的标准 android 可绘制背景在应用程序中不可见,但可以这样做:
TabSpec tc = getTabHost().newTabSpec("tag");
tc.setIndicator("indicator1");
tc.setContent(content);
getTabHost().addTab(tc);
getTabHost().findViewById(android.R.id.icon).setVisibility(View.GONE);
TextView tv = (TextView) getTabHost().findViewById(android.R.id.title);
RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rllp.addRule(RelativeLayout.CENTER_IN_PARENT);
tv.setLayoutParams(rllp);
然后可以降低标签的高度:
RelativeLayout parent = ((RelativeLayout) tv.getParent());
ViewGroup.LayoutParams vglp = parent.getLayoutParams();
vglp.height = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
parent.setLayoutParams(vglp);
结果将是:
关于android - 我可以在自定义选项卡 View 中使用默认选项卡样式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8271385/