2015-12-16 47 views

答えて

9

あなたはXMLでカスタムの背景を定義した後、右サイドから矢印のマージンを設定することによって、この問題を解決することができます。

まず、矢印の矩形の背景とビットマップオブジェクトでレイヤーリストを定義します。矢印を重心を設定することで右側の中心に合わせることができ、アンドロイド:右の属性で右マージンを設定することで、矢印を中心に移動させることができます。これは、テキストの長さに基づいて矢印を動的に移動することはありませんが、最初のステップとして役立つはずです。

spinner_background.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape android:shape="rectangle"> 
      <solid android:color="@color/color_white" /> 
      <corners android:radius="2.5dp" /> 
     </shape> 
    </item> 
    <item android:right="64dp"> 
     <bitmap 
      android:gravity="right|center_vertical" 
      android:src="@drawable/ic_spinner" /> 
    </item> 
</layer-list> 
+2

関心のある2つの側面が次のとおりです。1.Thereは閉じ括弧です(>)アンドロイドの後に行方不明:SRC = "@の描画可能/ ic_spinnerを"。 2. drawnerフォルダにspinner_background.xmlを作成する必要があります。 – XerXes

0

これは、選択されたスピナー項目custom_spinner_item.xmlのカスタムレイアウトを作成することによって達成することができます。現在選択されているスピナーアイテムを表示するTextViewを追加しました。矢印アイコンがImageViewに追加されます。任意のアイコンを使用できます。 TheoKanningの答えでは達成できないテキストの長さによって、矢印アイコンが動きます。実際には、このレイアウトでスピナーの外観を完全に変更することができます。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:orientation="horizontal"> 
    <TextView 
     android:id="@+id/spinner_item_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:paddingLeft="10dp" 
     android:paddingRight="10dp"/> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:src="@mipmap/ic_arrow_down"/> 
</LinearLayout> 

カスタムスピナーアダプターを作成し、上記のビューを膨張させます。また、デフォルトのgetView()メソッドをオーバーライドして、選択したスピナー項目のテキストをリストから設定します。

public class CustomSpinnerAdapter extends ArrayAdapter<String> { 
    LayoutInflater inflater; 
    List<String> spinnerItems; 

    public CustomSpinnerAdapter(Context applicationContext, int resource, List<String> spinnerItems) { 
     super(applicationContext, resource, spinnerItems); 
     this.spinnerItems = spinnerItems; 
     inflater = (LayoutInflater.from(applicationContext)); 
    } 
    @Override 
    public View getView(int i, View view, ViewGroup viewGroup) { 
     view = inflater.inflate(R.layout.custom_spinner_item, null); 
     TextView type = (TextView) view.findViewById(R.id.spinner_item_text); 
     type.setText(spinnerItems.get(i)); 
     return view; 
    } 
} 

CustomSpinnerAdapterクラスをインスタンス化し、スピナーのアダプタとして設定します。 spinnerListは、スピナーに表示される項目のリストです。

CustomSpinnerAdapter customSpinnerAdapter = new CustomSpinnerAdapter(getContext(), android.R.layout.simple_spinner_item, spinnerList); 
spinner.setAdapter(customSpinnerAdapter); 
-1
1) Set the background of spinner to @null 

<Spinner 
    android:id="@+id/spinner" 
    android:background="@null" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

2) In the spinner adapter layout, create an Imageview to the right of Textview or whatever content is in there 
change the visibility of the imageview to "gone" 

3)Override the onItemSelected method of the spinner in your activity, 
in the onItemSelected method, call the Imageview 
    ImageView downArrow = (ImageView) view.findViewById(R.id.down_arrow); 

change its visibility to "visible" 
関連する問題