• 自定义Spinner


    java逻辑代码:

            /* parity */
            paritySpinner = (Spinner) findViewById(R.id.parityValue);
            ArrayAdapter<CharSequence> parityAdapter = ArrayAdapter.createFromResource(this, R.array.parity,
                            R.layout.my_spinner_textview);
            parityAdapter.setDropDownViewResource(R.layout.my_spinner_textview);
            paritySpinner.setAdapter(parityAdapter);
            paritySpinner.setGravity(0x11);
            /* default is none */
            parity = 0;

    监听:

        /* set the adapter listeners for parity */
        paritySpinner.setOnItemSelectedListener(new MyOnParitySelectedListener());
    public class MyOnParitySelectedListener implements OnItemSelectedListener { public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { String parityString = new String(parent.getItemAtPosition(pos).toString()); if (parityString.compareTo("None") == 0) { parity = 0; } if (parityString.compareTo("Odd") == 0) { parity = 1; } if (parityString.compareTo("Even") == 0) { parity = 2; } if (parityString.compareTo("Mark") == 0) { parity = 3; } if (parityString.compareTo("Space") == 0) { parity = 4; } } public void onNothingSelected(AdapterView<?> parent) { // Do nothing. }} } }

    布局文件Spinner:R.id.parityValue

    <Spinner
                    android:id="@+id/parityValue"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_marginBottom="5dp"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"
                    android:layout_marginTop="5dp"
                    android:layout_weight="1"
                    android:background="#708070"
                    android:gravity="center_horizontal|center"
                    android:inputType="none" />

    下拉布局:R.layout.my_spinner_textview

    <?xml version="1.0" encoding="utf-8"?>
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        style="?android:attr/spinnerItemStyle"
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:gravity="center" />

    数据源:R.array.parity

        <string-array name="parity">        
            <item>None</item>        
            <item>Odd</item>
            <item>Even</item>
            <item >Mark</item>
            <item >Space</item>
        </string-array>

    效果图:

    这种样式需要设置:

           android:spinnerMode="dropdown"


    默认:

    android:spinnerMode="dialog"
  • 相关阅读:
    拓扑排序
    最短路径(Dijkstra,SPFA,Floyd)
    最小生成树(Prim)
    最长公共子序列(DP)(二种数组实现+扩展)
    HDU3068(最长回文串)
    python pip 阿里云内网安装地址
    python matplotlib画图改为可写中文
    win10 安装 basemap
    Liunx 安装basemap
    Docker 命令大全
  • 原文地址:https://www.cnblogs.com/xiaobijia/p/5377818.html
Copyright © 2020-2023  润新知