2016-06-02 28 views
1
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 =男性(アクティビティ)を選択すると、色は赤に変わり、reportType = Female(前のアクティビティから)の場合、色は青に変わります。色を変更TextViewには

私はstring.xmlを仕方カラーセットを使用することを考えて...セットカラーレッド男性用..しかし、私はそれを設定する正しい方法を知らない...

答えて

0

カスタムスピナーアダプタを作成する必要があります。 http://mrbool.com/how-to-customize-spinner-in-android/28286

しかし、あなたは簡単な解決策をしたい場合、あなたは男性と、このような、女性のための2つの別々のレイアウトを作成することができます:それを行う方法については、このページを見て、男性の場合は

を: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);} 
+0

あなたの答えは正しいですが、自分のコードでカスタマイズするには、決して色を変更しないでください。おそらく私は(私のコードで)スピナーを置き換えます。それは文字列配列の項目に自動的に色を設定できますか? –

+0

いいえ、文字列アイテムの色を定義することはできません。あなたは手動で設定しなければなりません。 –

+0

Alright Sir ..私のコードでは、アダプタとsimpleBArrayAdapterである2つのArrayAdapterを使用しました。ユーザーがFemaleを選択したときにこれを行います。女性を表示する位置が変更されます。新しいArrayAdapter(this、android.R .layout.simple_spinner_dropdown_item、type_report); .. ArrayAdapter.createFromResource(this、R.array.type_report、R.layout.item_females);} ... –

関連する問題