开发者

Android - Styling Spinner within TabView

开发者 https://www.devze.com 2023-03-30 11:37 出处:网络
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 whi

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 
        a开发者_C百科ndroid: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?


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号