2016-03-24 29 views
4

私は、灰色の背景色をスピナーとそのitemrowに追加しようとしています。文字色を青に変更し、スピナーの右にイメージを配置したいと考えています。ノートデバイスの色とタブデバイスの黒色。 私は非常にアンドロイドに新しいです。私を助けてください。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); 

答えて

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

使用この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> 

私はあなたを助けてくれることを願っています。何かエラーが見つかりましたらお知らせください

+0

このコードはandroid xamarinで動作しますか? –

+0

あなたは試してみることができます..少なくともあなたはそれからアイデアを得ることができます..そして、私はxamarinタグに気付かなかった。 –

+0

@Mustanser Iqbalはxmlデザインの上にitemviewsをlistviewでデザインしていますが、私は灰色の背景色で青いドロップダウンイメージ(黒ではなく)を使用したいと思います。変更するためにテーマを使用する必要がありますか? xamarinとandroidの両方でxmlのデザインが同じであるため、提案は大丈夫です。 – dafodil

関連する問題