Android的TabWidget自定义样式

giz*_*ero 14 android android-widget

我在我的Android应用程序中使用选项卡,在HTC Sense手机上运行应用程序时遇到了这个问题:

Android:TabWidget的突出显示选项卡在HTC Sense上无法读取

建议那里的解决方案(将android:targetSdkVersion设置为4)不能解决我的问题,也不想将目标sdk设置为4.

我已经尝试解决这个问题,我创建了自己的标签小部件样式,并修改了文本颜色.问题是当我使用自己的风格时没有明显的区别; 即样式似乎没有应用于选项卡.

这是主要活动的代码,包含选项卡:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab);

    tabHost = getTabHost();
    tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab 1").setContent(new Intent(this, Tab1.class)));
    tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab 2").setContent(new Intent(this, Tab2.class)));

    tabHost.setCurrentTab(0);
}
Run Code Online (Sandbox Code Playgroud)

这是我的tab.xml.请注意,我已将MyTabStyle指定为TabWidget的样式:

<?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="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp">
        <TabWidget
            style="@style/MyTabStyle"
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp" />
    </LinearLayout>
</TabHost>
Run Code Online (Sandbox Code Playgroud)

这是我对MyTabStyle的定义,我在res/values/styles.xml中定义了它:

<style name="MyTabStyle" parent="@android:style/TextAppearance.Widget.TabWidget">
    <item name="android:textColor">#5DFC0A</item>
</style>
Run Code Online (Sandbox Code Playgroud)

为什么我的应用程序中没有MyTabStyle中的任何更改?解决HTC Sense中选定选项卡上不可见文本的任何其他解决方案?

更新2011-06-02

通过使用选项卡上的文本实际上是TextViews的知识,我设法以一种hacky方式解决了这个问题.将以下方法添加到您的活动中:

private void setTabColor(TabHost tabHost) {
    try {
        for (int i=0; i < tabHost.getTabWidget().getChildCount();i++) {
            TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
            if (tv != null) {
                tv.setTextColor(Color.parseColor("#ffffff"));
            }
            TextView tv2 = (TextView) tabHost.getCurrentTabView().findViewById(android.R.id.title); // Selected Tab
            if (tv2 != null) {
                tv2.setTextColor(Color.parseColor("#000000"));
            }
        }
    } catch (ClassCastException e) {
        // A precaution, in case Google changes from a TextView on the tabs.
    }
}
Run Code Online (Sandbox Code Playgroud)

在您的活动中的onCreate()方法中添加以下内容:

// Only apply the fix if this is a HTC device.
if (android.os.Build.MANUFACTURER.toLowerCase().contains("htc")) {
    // Set up the color initially
    setTabColor(tabHost);

    // Add a tab change listener, which calls a method that sets the text color.
    tabHost.setOnTabChangedListener(new OnTabChangeListener() {
        public void onTabChanged(String tabId) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(tabHost.getApplicationWindowToken(), 0);
            setTabColor(tabHost);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

Fra*_*gle 11

这家伙发布了一种方法来定制关于标签的所有内容.适用于Android 1.6及更高版本:自定义Android标签.

我还没有尝试过,但到了那里.基本上你在TabSpec上调用setIndicator并传递一个视图而不仅仅是文本.然后,您可以使用选择器等自定义该视图.

  • "这个家伙"是我. (34认同)

Moh*_*hit -6

<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_focus" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_focus" />

<!-- Pressed -->
<item android:state_pressed="true" android:drawable="@drawable/tab_press" />
Run Code Online (Sandbox Code Playgroud)

  • 这如何回答我的问题? (9认同)