下拉式選單的選單樣式
註:增加padding可以讓選單的間距變寬
建立ArrayAdapter,放入我們想要顯示樣式及選項內容
如果是想要用Android內建的樣式,只要把R.layout.myspinner改回android.R.layout.simple_list_item_1就可以了
最後設定Spinner的Adapter為剛剛我們建立的ArrayAdapter
完整的程式碼:
執行結果:
Step1 在activity_main.xml檔案中新增Spinner標籤
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Spinner | |
android:id="@+id/spinner" | |
android:layout_width="300dp" | |
android:layout_height="wrap_content" | |
android:gravity="center" | |
android:layoutMode="clipBounds" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
android:layout_marginTop="16dp"></Spinner> |
Step2 在layout中新增myspinner.xml
在檔案中設定下拉式選單的TextView樣式 (文字顏色、大小等)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<TextView xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="5dp" | |
android:textColor="@color/colorAccent" | |
android:textSize="16sp" | |
android:paddingTop="10dp" | |
android:paddingBottom="10dp" | |
android:paddingLeft="16dp" | |
android:gravity="center_vertical"/> |
Step3 在MainActivity.java檔案中設定Spinner
宣告spinner,並建立string array放入下拉選單中選項內容
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spinneer = (Spinner) findViewById(R.id.spinner); | |
final String[] item = {"A", "B", "C", "D", "E", "F"}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ArrayAdapter<String> itemlist = new ArrayAdapter<>(this,R.layout.myspinner, | |
item); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ArrayAdapter<String> itemlist = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, | |
item); |
完整的程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Spinner spinneer; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
spinneer = (Spinner) findViewById(R.id.spinner); | |
final String[] item = {"A", "B", "C", "D", "E", "F"}; | |
ArrayAdapter<String> itemlist = new ArrayAdapter<>(this,R.layout.myspinner, | |
item); | |
spinneer.setAdapter(itemlist); | |
} |
執行結果:
留言
張貼留言