일반적인 String 스피너 만들기
- SpinnerActivity.java
public class SpinnerActivity extends BaseActivity {
private String[] mCategoryArr = null;
private Spinner mSubjectSpinner = null;
private String mSelectCategory = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.spinner_activity);
initView();
}
private void initView() {
mSubjectSpinner = (Spinner) findViewById(R.id.subject_spinner);
mSubjectSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
switch (position) {
case 0:
mSelectCategory = mCategoryArr[0];
break;
case 1:
mSelectCategory = mCategoryArr[1];
break;
case 2:
mSelectCategory = mCategoryArr[2];
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
private void settingView() {
mCategoryArr = getResources().getStringArray(R.array.subject_array);
mSelectCategory = mCategoryArr[0];
ArrayAdapter<CharSequence> spinnerLargerAdapter = ArrayAdapter.createFromResource(this, R.array.subject_array, R.layout.spinner_item);
mSubjectSpinner.setAdapter(spinnerLargerAdapter);
mSubjectSpinner.setSelection(0);
}
}
- R.layout.spinner_activity.xml
#저 같은 경우 @drawable/ic_keyboard_arrow_down_black_24dp을 drawable-hdpi폴더에만 넣어놨습니다.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<Spinner
android:id="@+id/free_subject_spinner"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:background="@drawable/ic_keyboard_arrow_down_black_24dp"
/>
</LinearLayout>
- R.layout.spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/spinner_title"
android:layout_height="50dp"
android:paddingLeft="14.62dp"
android:textSize="16dp"
android:textColor="@color/black_color"
android:singleLine="true"
android:ellipsize="marquee"
android:background="@drawable/custom_menu_item_bg"
android:gravity="center_vertical"/>
res\drawable\custom_menu_item_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#e6e6e6"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#ffffff"/>
</shape>
</item>
</selector>
res\values\arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="subject_array">
<item>[건의]</item>
<item>[후기]</item>
<item>[기타]</item>
</string-array>
</resources>
'Android' 카테고리의 다른 글
안드로이드 WheelView or StringPicker (0) | 2017.03.03 |
---|---|
안드로이드 키보드 이벤트 (0) | 2017.02.23 |
안드로이드 액티비티 애니메이션 (0) | 2016.11.14 |
RecyclerView 만들기 (0) | 2016.11.14 |
Activity와 Fragment간 데이터 전달 (0) | 2016.11.14 |