【问题标题】:How to set a custom spinner?如何设置自定义微调器?
【发布时间】:2014-03-22 23:43:40
【问题描述】:

我想更改微调器中项目的文本颜色,因此我创建了 custom_spinner.xml,如下所示:

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

 <LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/spinner_item_linear_layout" 
   android:layout_width="match_parent" 
   android:layout_height="wrap_content"
   android:orientation="vertical">


<TextView        
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:textColor="@color/White"
  android:background="@color/Blue"
  android:id="@+id/spinner_textView" >        
</TextView>



 </LinearLayout>

我认为这没问题。但问题是,当我想将此自定义微调器提供给 ArrayAdapter 时,Eclipse 会抛出“my_spinner 无法解析或不是字段”异常。我的代码如下:

    ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
    android.R.layout.my_spinner, projectList);

感谢您的帮助!

【问题讨论】:

  • android.R 表示 R 在 android 包中。你的 R 不在 android 包中。

标签: java android spinner android-spinner


【解决方案1】:

改成

   ArrayAdapter<String> aa = new ArrayAdapter<String>(this,R.layout.my_spinner, projectList);

R 应该是你的 R.java // 检查你的导入

查看此示例以获取自定义微调器 -

http://androidexample.com/Custom_Spinner_With_Image_And_Text_-_Android_Example/index.php?view=article_discription&aid=84&aaid=107

【讨论】:

  • 谢谢,eclipse 现在不抛出异常了。但是当我使用这个 ArrayAdapter 启动我的应用程序时,它会崩溃。但是当我使用系统微调器(android.R.layout.simple_spinner_dropdown_item)启动它时,它可以工作。你现在有什么问题吗?非常感谢您的帮助
  • 您必须使用自定义适配器才能使用自定义微调器布局。检查此链接.. androidexample.com/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多