android - 选项卡下的 strip 未绘制

标签 android tabs strip

在我的 Activity 中,我这样做是为了设置 TabHost:

//...     
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.getTabWidget().setDividerDrawable(R.drawable.my_divider_tab);
        mTabHost.getTabWidget().setStripEnabled(true);
        mTabHost.getTabWidget().setLeftStripDrawable(R.drawable.my_strip_tab);
        mTabHost.getTabWidget().setRightStripDrawable(R.drawable.my_strip_tab);
        setupTab(new TextView(this), getString(R.string.device_text_tab));
        setupTab(new TextView(this), getString(R.string.sensor_text_tab));
        setupTab(new TextView(this), getString(R.string.actuator_text_tab));
//...

private void setupTab(final View view, final String tag) {
    
    View tabview = createTabView(mTabHost.getContext(), tag);
        TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
        public View createTabContent(String tag) {return view;}
    });
    mTabHost.addTab(setContent);
}

private static View createTabView(final Context context, final String text) {
    
    View view = LayoutInflater.from(context).inflate(R.layout.state_tabwidget, null);
    TextView tv = (TextView) view.findViewById(R.id.tabsText);
    tv.setText(text);
    return view;
}

my_strip_tab.xml 只是一个矩形形状:

<?xml version="1.0" encoding="utf-8"?>
<shape  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    
    <solid 
        android:color="#FFFFFF"/>
    
    <size 
        android:width="0dp"
        android:height="2dp"/>
</shape>

绘制垂直分隔线。但选项卡下的 strip 未显示...

可能是什么问题? my_strip_tab 的大小?

我刚刚发现:

/**
 * Controls whether the bottom strips on the tab indicators are drawn or
 * not.  The default is to draw them.  If the user specifies a custom
 * view for the tab indicators, then the TabHost class calls this method
 * to disable drawing of the bottom strips.
 * @param stripEnabled true if the bottom strips should be drawn.
 */

这里:TabWidgetExample

所以看起来当我们使用选项卡的自定义 View 时(就像我正在做的那样),该 strip 会自动禁用...... 我怎样才能启用它?

最佳答案

您唯一要做的就是移动这段代码

    mTabHost.getTabWidget().setStripEnabled(true);
    mTabHost.getTabWidget().setLeftStripDrawable(R.drawable.my_strip_tab);
    mTabHost.getTabWidget().setRightStripDrawable(R.drawable.my_strip_tab);

在你的setupTab()之后

原因是,如果您查看 Google 文档,就会发现当您使用选项卡指示器的自定义 View 时,将自动调用 setStripEnabled(false)。您只需在设置所有自定义 View 或选项卡指示器后再次重新启用它即可。

关于android - 选项卡下的 strip 未绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10124973/

相关文章:

android - 如何阻止 Android 选项卡中的标题在单词中间换行?

Python 从 DataFrame 字符串字段中去除所有空格

Android 对话框 - 自定义背景而不是变暗或模糊

android - 当回收器 View 放置在 nestedscrollview 中时,PagedListAdapter 获取所有项目

android - 以一种聪明的方式根据风格排除 JNI 库

Android Textview背景颜色资源

html - 纯 CSS 选项卡 - 防止在选项卡更改时滚动到页面顶部

JavaScript 制作标签

php - 如何转义/去除 LaTeX 文档中的特殊字符?

c++ - 如何从字符串中去除非 ASCII 字符?在 C++ 中