zoukankan      html  css  js  c++  java
  • 自定义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"
  • 相关阅读:
    Android系统介绍与框架(转)
    6个值得推荐的Android开源框架简介(转)
    程序员最喜爱的12个Android应用开发框架二(转)
    android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法
    Android数据缓存(转)
    [UI]实用案例--Shape绘制实用圆圈
    接口API测试和返回值JSON解析的插件
    Android LayoutInflater详解(转)
    一个json字符串
    Android中设定EditText的输入长度(转)
  • 原文地址:https://www.cnblogs.com/xiaobijia/p/5377818.html
Copyright © 2011-2022 走看看