【问题标题】:Change color textview in adapter spinner android在适配器微调器android中更改颜色文本视图
【发布时间】:2016-06-02 08:09:53
【问题描述】:
  reports = (Spinner)findViewById(R.id.spinner_report);
  reportType = getIntent().getStringExtra("report");

   private void setTypeReport(){
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.type_report, R.layout.item_spinner_p);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    reports.setAdapter(adapter);

    int pos= adapter.getPosition(reportType);
    type_report= getResources().getStringArray(R.array.type_report);

    String valueAtIndex = type_report[pos];
    for(int i = pos; i > 0; i--){
        type_report[i] = type_report[i-1];
    }
    type_report[0] = valueAtIndex;
    //now set this array to second Spinner
    ArrayAdapter spinnerBArrayAdapter = new ArrayAdapter(this,
            android.R.layout.simple_spinner_dropdown_item,
            type_report);
    reports.setAdapter(spinnerBArrayAdapter);
    newreport = reports.getSelectedItem().toString();
}
               main.xml
               <?xml version="1.0" encoding="utf-8"?>
               <RelativeLayout
               xmlns:android="http://schemas.android.com/apk/res/android"
               android:layout_width="match_parent"
               android:background="#eee"
               android:descendantFocusability="beforeDescendants"
               android:focusableInTouchMode="true"
               android:layout_height="match_parent">

                 <Spinner
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/spinner_report"
                    android:background="@drawable/round_box"
                    android:visibility="gone"
                    android:layout_marginBottom="5dp">
                </Spinner>
               </RelattiveLayout>


     item_spinner_p.xml 

     <TextView xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@android:id/text1"
       android:layout_width="match_parent"
        android:layout_height="wrap_content"
       android:textColor="@color/black"
       android:textSize="17sp"
       android:paddingTop="10dp"
       android:paddingBottom="10dp"
       android:paddingLeft="7dp"
       android:paddingRight="7dp"/>



      string.xml
<string-array name="type_report">
    <item>Male</item>
    <item>Female</item>

</string-array>

item_spinner_p.xml 中的默认颜色是黑色...无论是男性还是女性,它都会显示黑色。问题是,我想在 reportType = Male(来自之前的活动)时执行此操作,它会将颜色更改为红色并且如果 reportType = 女性(来自之前的活动),它会将颜色更改为蓝色。

我认为在 string.xml 中使用方式设置颜色 .. 为男性设置颜色红色 .. 但我不知道设置它的正确方法..

【问题讨论】:

    标签: android colors textview spinner


    【解决方案1】:

    您必须创建自定义微调器适配器。查看此页面以了解如何执行此操作:http://mrbool.com/how-to-customize-spinner-in-android/28286

    但如果您想要简单的解决方案,您可以像这样为男性和女性创建两个单独的布局:

    对于男性:item_males

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@android:id/text1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:textColor="@android:color/red"
       android:textSize="17sp"
       android:paddingTop="10dp"
       android:paddingBottom="10dp"
       android:paddingLeft="7dp"
       android:paddingRight="7dp"/>
    

    对于女性:item_females

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@android:id/text1"
       android:layout_width="match_parent"
        android:layout_height="wrap_content"
       android:textColor="@android:color/blue"
       android:textSize="17sp"
       android:paddingTop="10dp"
       android:paddingBottom="10dp"
       android:paddingLeft="7dp"
       android:paddingRight="7dp"/>
    

    男性和女性的文字颜色分别设置为红色和蓝色。然后在设置适配器时应用检查:

    ArrayAdapter<CharSequence> adapter;
    if(reportType.equals("Males")){
    adapter = ArrayAdapter.createFromResource(this,R.array.type_report, R.layout.item_males);}
    else{
    adapter = ArrayAdapter.createFromResource(this,R.array.type_report, R.layout.item_females);}
    

    【讨论】:

    • 你的答案是正确的,但是用我的代码定制,它永远不会改变颜色..也许我确实替换了微调器..(在我的代码中)真的很累定制它..我们可以自动设置颜色吗字符串数组中的项目 .. ?
    • 不,我们不能为字符串项目定义颜色。您必须通过我提到的任何一种方法手动设置它。
    • 好吧,先生.. 在我的代码中,我使用了两个 ArrayAdapter,即适配器和 simpleBArrayAdapter.. 我这样做是因为当用户选择女性时,它会改变位置以显示女性.. 所以需要使用新的ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,type_report); .. 它与 ArrayAdapter.createFromResource(this,R.array.type_report, R.layout.item_females);} ... 不同
    • 当我将 simpleBArrayAdapter 更改为 ArrayAdapter.createFromResource(this,R.array.type_report, R.layout.item_females);} 时,它成功更改颜色,但它首先显示男性,甚至用户选择女性
    • 在设置适配器时将微调器移动到选定的项目,在设置适配器后使用reports.setSelection(0) 用于男性或reports.setSelection(1) 用于女性。
    猜你喜欢
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    相关资源
    最近更新 更多