2016-03-24 17 views
4

Ich versuche, eine graue Hintergrundfarbe zu Spinner und seine itemrow hinzuzufügen, Textfarbe zu blau ändern und Bild rechts in Spinner platzieren möchte .Zurzeit bekomme ich weiß Farbe im Notizgerät und schwarze Farbe im Tab-Gerät. ich bin sehr neu zu Android bitte hilf mir.Hinzufügen von Hintergrundfarbe und Bild zu Spinner in android Xamarin

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <Spinner 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/moviesSpinner" 
     android:prompt="@string/movie_prompt" /> 
    <ImageView 
     android:src="@android:drawable/Icon" 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:id="@+id/imageView1" /> 
</LinearLayout> 

itemrow.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:background="#ffffff" 
    android:padding="3dip"> 
    <TextView 
     android:padding="3dip" 
     android:layout_marginTop="2dip" 
     android:textColor="#C11B17" 
     android:textStyle="bold" 
     android:id="@+id/company" 
     android:layout_marginLeft="5dip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</RelativeLayout> 

mainActivity.cs

var moviesSpinner1 = FindViewById<Spinner>(Resource.Id.moviesSpinner); 
      moviesSpinner1.Adapter = new MoviesAdapter(this, MoviesRepository.Movies); 

Antwort

0
public class SpinnerAdapter extends ArrayAdapter<SpinnerItem> { 
    private Context mContext; 
    private ArrayList<SpinnerItem> listState; 

    public SpinnerAdapter(Context context, int resource, 
      ArrayList<SpinnerItem> objects) { 
     super(context, resource, objects); 
     this.mContext = context; 
     this.listState = objects; 
    } 

    @Override 
    public View getDropDownView(int position, View convertView, ViewGroup parent) { 
     return getCustomView(position, convertView, parent, true); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     return getCustomView(position, convertView, parent, false); 
    } 

    @SuppressLint("InflateParams") 
    public View getCustomView(int position, View convertView, ViewGroup parent, boolean isDropDown) { 
     final ViewHolder holder; 
     if (convertView == null) { 
      LayoutInflater layoutInflator = LayoutInflater.from(mContext); 
      convertView = layoutInflator.inflate(R.layout.spinner_item_layout, 
        null); 
      holder = new ViewHolder(); 
      holder.mTextView = (TextView) convertView.findViewById(R.id.text); 
      holder.mCheckedImage = (ImageView) convertView 
        .findViewById(R.id.checkbox); 
      holder.mBgLayout= (RelativeLayout) convertView 
        .findViewById(R.id.bg); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.mTextView.setText(listState.get(position).getTitle()); 

     if (isDropDown) { 
     if (listState.get(position).isSelected()) { 
      holder.mCheckedImage.setVisibility(View.VISIBLE); 
      holder.mBgLayout.setBackgroundColor(android.Color.BLUE); // not sure if the syntax is correct. you need to check this line 
     } else { 
      holder.mCheckedImage.setVisibility(View.INVISIBLE); 
      holder.mBgLayout.setBackgroundColor(android.Color.YELLOW); // not sure if the syntax is correct. you need to check this line 
     } 
} 

     return convertView; 
    } 

    private class ViewHolder { 
     private TextView mTextView; 
     private ImageView mCheckedImage; 
     private RelativeLayout mBgLayout; 
    } 

    public void setSpinnerAdapter(ArrayList<SpinnerItem> spinnerItems) { 

     this.listState = spinnerItems; 
     notifyDataSetChanged(); 
    } 
} 


private void setBottelCountData() { 
     final String[] select_qualification = { "", "1", "2", 
       "3" }; 

     bottelCountList = new ArrayList<>(); 

     for (int i = 0; i < select_qualification.length; i++) { 
      SpinnerItem spinnerItem = new SpinnerItem(); 
      spinnerItem.setTitle(select_qualification[i]); 
      if (i == 2) { 

       spinnerItem.setSelected(false); 
      } else { 
       spinnerItem.setSelected(false); 
      } 
      bottelCountList.add(spinnerItem); 
     } 

     bottelCountAdapter = new SpinnerAdapter(getActivity(), 0, 
       bottelCountList); 
     bottelCountAdapter.setDropDownViewResource(R.layout.spinner_item_layout); 
     bottel_Count_Spinner.setAdapter(bottelCountAdapter); 
     bottel_Count_Spinner.setSelection(3); 
    } 



bottel_Count_Spinner 
       .setOnItemSelectedListener(new OnItemSelectedListener() { 

        public void onItemSelected(AdapterView<?> parent, 
          View view, int position, long id) { 
         if (position == 0) { 
          for (int count = 0; count < bottelCountList.size(); count++) { 
           bottelCountList.get(count).setSelected(false); 
          } 
          noOfBottel = 0; 
          return; 
         } 
         bottelCountList.get(position).setSelected(true); 
         for (int count = 0; count < bottelCountList.size(); count++) { 
          if (position != count) { 
           bottelCountList.get(count).setSelected(false); 
          } 
         } 
         bottel_Count_Spinner.setPrompt("Hello"); 
         bottelCountAdapter.setSpinnerAdapter(bottelCountList); 
         noOfBottel = Integer.parseInt(bottelCountList.get(
           position).getTitle()); 
        } 

        public void onNothingSelected(AdapterView<?> parent) { 
        } 
       }); 

Verwendung dieses xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:id="@+id/bg" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/checkbox" 
     android:layout_centerVertical="true" 
     android:padding="10dp"/> 

    <ImageView 
     android:id="@+id/checkbox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:padding="10dp" 
     android:src="@android:drawable/checkbox_on_background" 
     android:contentDescription="@string/app_name" 
     android:layout_marginLeft="10dp" 
     android:layout_alignParentStart="true" /> 

</RelativeLayout> 

ich hoffe, dass dir das weiterhilft. Wenn Sie einen Fehler finden, lassen Sie wissen,

+0

dieser Code in Android Xamarin arbeiten? –

+0

Sie können es versuchen .. zumindest können Sie die Idee von ihm .. und ich habe nicht die Xamarin-Tag. –

+0

@Mustanser Iqbal über Xml Design Design der Artikel in Listview, aber ich möchte Spinner mit grauer Hintergrundfarbe, mit blauen Dropdown-Bild (statt schwarz) .Soll ich jedes Thema zu ändern scheint von Spinner oder Android-Unterstützung Material Design-Tool.Any Vorschlag ist in Ordnung, wie Design in Xml ist in Xamarin und Android identisch. – dafodil