Android - TabView 中的微调器样式

发布于 2024-11-30 23:34:03 字数 4207 浏览 3 评论 0原文

我有一个使用选项卡进行导航的应用程序,其中一个选项卡上有一个微调器。但是,当选择微调器并出现实际选择窗口时,所有文本都是白色背景上的白色。 我尝试过设计布局的样式,但我所做的一切都没有改变字体的颜色。

主类

public class RealmsOfWickedry extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab);

        TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);

        TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
        TabSpec secondTabSpec = tabHost.newTabSpec("tid1");

        firstTabSpec.setIndicator("Home").setContent(new Intent(this,FirstTab.class));
        secondTabSpec.setIndicator("Catalog").setContent(new Intent(this,SecondTab.class));

        tabHost.addTab(firstTabSpec);
        tabHost.addTab(secondTabSpec);
    }

    public static View makeSpinner(Context context) {
        View v = LayoutInflater.from(context).inflate(R.layout.spinner, null);
        Spinner spinner = (Spinner) v.findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item);
        adapter.add("Item 1");
        adapter.add("Item 2");
        adapter.add("Item 3");
        adapter.add("Item 4");
        spinner.setAdapter(adapter);
        return v;
    }
}

的类

public class SecondTab extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /* Second Tab Content */
        TextView textView = new TextView(this);
        textView.setText("Choose a Category");
        setContentView(textView);
        setContentView(RealmsOfWickedry.makeSpinner(getParent())); 
    }
}

带有 spinner tab.xml

<?xml version="1.0" encoding="utf-8"?>

<TabHost android:layout_width="fill_parent"
    android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost">
    <LinearLayout android:id="@+id/LinearLayout01"
        android:orientation="vertical" android:layout_height="fill_parent"
        android:layout_width="fill_parent">
        <TabWidget android:id="@android:id/tabs"
            android:layout_height="wrap_content" android:layout_width="fill_parent"></TabWidget>
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_height="fill_parent" android:layout_width="fill_parent"></FrameLayout>
    </LinearLayout>

</TabHost>

spinner.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Spinner 
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/cat_prompt"
        android:theme="@style/DropdownStyle"
    />
</LinearLayout>

theme.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="OverallStyle" parent="@android:Theme.Light">
        <item name="android:windowBackground">@drawable/bg</item>
        <item name="android:textColor">@color/white</item>
    </style>
    <style name="WelcomeStyle" parent="@android:Theme.Light">
        <item name="android:typeface">monospace</item>
        <item name="android:gravity">center</item>
    </style>
    <style name="CustomStyle" parent="@android:Theme.Light">
        <item name="android:typeface">monospace</item>
        <item name="android:gravity">top</item>
    </style>
    <style name="DropdownStyle" parent="@android:Theme.Light">
        <item name="android:textColor">@color/red</item>
    </style>
</resources>

谁能帮我解决这个问题?

I've got an app that uses tabs for navigation, and on one of those tabs there is a spinner. However, when the spinner is selected and the actual select window comes up, all the text is white on a white background.
I've tried styling the layout but nothing I do changes the color of the font.

the main class

public class RealmsOfWickedry extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab);

        TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);

        TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
        TabSpec secondTabSpec = tabHost.newTabSpec("tid1");

        firstTabSpec.setIndicator("Home").setContent(new Intent(this,FirstTab.class));
        secondTabSpec.setIndicator("Catalog").setContent(new Intent(this,SecondTab.class));

        tabHost.addTab(firstTabSpec);
        tabHost.addTab(secondTabSpec);
    }

    public static View makeSpinner(Context context) {
        View v = LayoutInflater.from(context).inflate(R.layout.spinner, null);
        Spinner spinner = (Spinner) v.findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item);
        adapter.add("Item 1");
        adapter.add("Item 2");
        adapter.add("Item 3");
        adapter.add("Item 4");
        spinner.setAdapter(adapter);
        return v;
    }
}

the class with the spinner

public class SecondTab extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /* Second Tab Content */
        TextView textView = new TextView(this);
        textView.setText("Choose a Category");
        setContentView(textView);
        setContentView(RealmsOfWickedry.makeSpinner(getParent())); 
    }
}

tab.xml

<?xml version="1.0" encoding="utf-8"?>

<TabHost android:layout_width="fill_parent"
    android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost">
    <LinearLayout android:id="@+id/LinearLayout01"
        android:orientation="vertical" android:layout_height="fill_parent"
        android:layout_width="fill_parent">
        <TabWidget android:id="@android:id/tabs"
            android:layout_height="wrap_content" android:layout_width="fill_parent"></TabWidget>
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_height="fill_parent" android:layout_width="fill_parent"></FrameLayout>
    </LinearLayout>

</TabHost>

spinner.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Spinner 
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/cat_prompt"
        android:theme="@style/DropdownStyle"
    />
</LinearLayout>

themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="OverallStyle" parent="@android:Theme.Light">
        <item name="android:windowBackground">@drawable/bg</item>
        <item name="android:textColor">@color/white</item>
    </style>
    <style name="WelcomeStyle" parent="@android:Theme.Light">
        <item name="android:typeface">monospace</item>
        <item name="android:gravity">center</item>
    </style>
    <style name="CustomStyle" parent="@android:Theme.Light">
        <item name="android:typeface">monospace</item>
        <item name="android:gravity">top</item>
    </style>
    <style name="DropdownStyle" parent="@android:Theme.Light">
        <item name="android:textColor">@color/red</item>
    </style>
</resources>

Can anyone help me out with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸵鸟症 2024-12-07 23:34:03

看起来您正在尝试覆盖系统主题以显示不同的颜色,这是正确的路径。您的 spinner xml 包含对 android:theme 的引用,我之前没有看到过该引用,而且它似乎不是此小部件 API 的一部分。要使 DropdownStyle 正常工作,首先将其添加为整体样式样式的一部分,项目名称为 @android:attr/spinnerDropDownItemStyle。其次,将 DropdownStyle 的父级更改为 @android:Widget.DropDownItem.Spinner。我假设 OverallStyle 已应用于清单中的活动或应用程序。这将更改所有微调器下拉项目的样式。

要仅应用于此视图的下拉项,只需执行上面的第二步,然后将 style="@style/OverallStyle" 添加到其布局中的微调器中。

附加信息:

<style name="DropdownStyle" parent="@android:Widget.DropDownItem.Spinner">
    <item name="android:textColor">@color/red</item>
</style>
<style name="OverallStyle" parent="@android:Theme.Light">
    <item name="android:windowBackground">@drawable/bg</item>
    <item name="@android:attr/spinnerDropDownItemStyle">@style/DropdownStyle</item>
    <item name="android:textColor">@color/white</item>
</style>

-OR-

主题.xml

<style name="DropdownStyle" parent="@android:Widget.DropDownItem.Spinner">
    <item name="android:textColor">@color/red</item>
</style>

spinner.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Spinner 
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/cat_prompt"
        style="@style/DropdownStyle"
    />
</LinearLayout>

It looks like you're trying to override the system theme to show a different color which is the right path to be on. Your spinner xml contains a reference to android:theme I haven't see that one before and it doesn't appear to be part of the API for this widget. To get your DropdownStyle to work, first, add it as part of your OverallStyle style with an item name of @android:attr/spinnerDropDownItemStyle. Second, change DropdownStyle's parent to @android:Widget.DropDownItem.Spinner. I'm assuming that OverallStyle is applied to the Activity or Application in the Manifest already. This will change the style for all Spinner drop down Items.

To apply only to this view's drop down items do only step two above then add style="@style/OverallStyle" to the spinner in its layout.

Additional Information:

<style name="DropdownStyle" parent="@android:Widget.DropDownItem.Spinner">
    <item name="android:textColor">@color/red</item>
</style>
<style name="OverallStyle" parent="@android:Theme.Light">
    <item name="android:windowBackground">@drawable/bg</item>
    <item name="@android:attr/spinnerDropDownItemStyle">@style/DropdownStyle</item>
    <item name="android:textColor">@color/white</item>
</style>

-OR-

themes.xml

<style name="DropdownStyle" parent="@android:Widget.DropDownItem.Spinner">
    <item name="android:textColor">@color/red</item>
</style>

spinner.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Spinner 
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/cat_prompt"
        style="@style/DropdownStyle"
    />
</LinearLayout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文