【问题标题】:Android - Styling Spinner within TabViewAndroid - TabView 中的样式微调器
【发布时间】:2011-11-02 08:45:28
【问题描述】:

我有一个使用标签进行导航的应用,其中一个标签上有一个微调器。 However, when the spinner is selected and the actual select window comes up, all the text is white on a white background. 我尝试过设计布局样式,但我没有做任何事情来改变字体的颜色。

主类

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())); 
    }
}

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>

主题.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>

谁能帮我解决这个问题?

【问题讨论】:

  • 您使用的是制造商 UI 还是 Google?什么代码创建了对话框?
  • 我只使用 Google 的 API。创建微调器的行是setContentView(RealmsOfWickedry.makeSpinner(getParent()));,它在主类中定义
  • 具体的制造商主题,例如 Sense、Touch Wiz、Blur、Google?
  • 老实说,我不确定。谈到 Java 和 Android,我仍然是个菜鸟。我会假设是 Google,因为我没有添加任何第三方插件或任何东西。
  • 虚拟设备模拟器。我还没有将 pkg 添加到我的实际设备中。

标签: java android jakarta-ee spinner tabview


【解决方案1】:

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

要仅应用于此视图的下拉项,只需执行上述第二步,然后将 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>

-或-

主题.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>

【讨论】:

  • 我不确定您所说的“将 [DropdownStyle] 添加为您的OverallStyle 样式的一部分,名称为...”。你能提供一个语法例子吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多