简体   繁体   English

在适配器微调器android中更改颜色textview

[英]Change color textview in adapter spinner android

  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>

Default color is black in item_spinner_p.xml ... either male or female , it will show black color.. Problem is , I want to do when reportType = Male (from previous activity), it will change color to Red and if reportType = Female (from previous activity) , it change color to Blue. item_spinner_p.xml中的默认颜色为黑色...无论是男性还是女性,它都会显示黑色。问题是,我想在reportType =男性(来自上一个活动)时做,它会将颜色更改为红色,如果reportType =女性(来自之前的活动),它将颜色变为蓝色。

I think to use way set color in string.xml .. set color Red for Male .. but i dont know correct way to set it .. 我想在string.xml中使用方式设置颜色..为男性设置颜色红色..但我不知道设置它的正确方法..

You have to create custom spinner adapter. 您必须创建自定义微调器适配器。 Look at this page for how to do that: http://mrbool.com/how-to-customize-spinner-in-android/28286 请看这个页面: http//mrbool.com/how-to-customize-spinner-in-android/28286

But if you want simple solution you can create two separate layouts for male and females like this : 但是如果你想要简单的解决方案,你可以为男性和女性创建两个单独的布局,如下所示:

For males: item_males 对于男性: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"/>

And for females: item_females 对于女性: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"/>

With text colors set to red and blue for males and females respectively. 文本颜色分别设置为红色和蓝色,分别为男性和女性。 Then apply a check while setting the adapter like this : 然后在设置适配器时应用检查,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM