美文网首页
Android控件之Spinner简单使用

Android控件之Spinner简单使用

作者: Killshadow | 来源:发表于2019-03-28 14:45 被阅读0次

    0x00 效果图


    0x01 定义Spinner视图

    drawable包下, 新建一个xml文件spinner_background.xml, 自定义Spinner的背景:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <layer-list>
                <item>
                    <shape>
                        <gradient android:angle="90"
                                  android:endColor="@color/moccasin"
                                  android:startColor="#E8EBEF"
                                  android:type="linear" />
    
                        <stroke android:width="1dp"
                                android:color="#000000" />
    
                        <corners android:radius="2dp" />
    
                        <padding android:bottom="3dp"
                                 android:left="3dp"
                                 android:right="3dp"
                                 android:top="3dp" />
                    </shape>
                </item>
            </layer-list>
        </item>
    </selector>
    
    (渐变色效果)效果如下:

    0x02 定义一个Theme

    res/values/styles.xml中添加一个style, 然后在Spinner添加这个Theme:

    <style name="spinnerstyle">
        <item name="android:background">@drawable/spinner_background</item>
        <item name="android:layout_marginLeft">10dp</item>
        <item name="android:layout_marginRight">10dp</item>
        <item name="android:layout_marginBottom">10dp</item>
        <item name="android:paddingLeft">0dp</item>
        <item name="android:paddingTop">3dp</item>
        <item name="android:paddingBottom">5dp</item>
        <item name="android:popupBackground">@color/lemon_chiffon</item>
    </style>
    

    0x03 添加Spinner控件

    在想要添加Spinner控件的layout里添加Spinner控件:

    <Spinner
        android:id="@+id/spinner_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:focusableInTouchMode="true"
        android:scrollbars="vertical"
        android:theme="@style/spinnerstyle">
    </Spinner>
    

    0x04 添加Spinner里的选项字符串数组

    这里我在res/values中添加了一个str_array_services.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="services_array">
            <item>@string/str_select_service</item>
            <item>@string/str_health_self</item>
            <item>@string/str_hs_project</item>
            <item>@string/str_com_care</item>
            <item>@string/str_find_people</item>
            <item>@string/str_dormitory</item>
        </string-array>
    </resources>
    

    那些@string/xxxstrings.xml定义这些字符串.(这里为了更好的耦合性, 可以设置成多个语言版本)


    0x05 在Activity或Fragment中实例化Spinner

    public class LoginActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
        private Spinner mSpinner;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
            mSpinner = findViewById(R.id.spinner_service);
            mSpinner.setOnItemSelectedListener(this);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                    R.array.services_array, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
            mSpinner.setAdapter(adapter);
        }
    
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    
        }
    
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // Another interface callback
        }
    }
    

    相关文章

      网友评论

          本文标题:Android控件之Spinner简单使用

          本文链接:https://www.haomeiwen.com/subject/ogyqbqtx.html