荣耀彩票代理

IT技术互动交流平台

AndroidSpinner控件详解

来源:IT165收集  发布日期:2016-06-27 21:13:36

SpinnerTIGONGLECONGYIGESHUJUJIHEZHONGKUAISUXUANZEYIXIANGZHIDEBANFA。MORENQINGKUANGXIASpinnerXIANSHIDESHIDANGQIANXUANZEDEZHI,DIANJISpinnerHUIDANCHUYIGEBAOHANSUOYOUKEXUANZHIDEdropdownCAIDAN,CONGGAICAIDANZHONGKEYIWEISpinnerXUANZEYIGEXINZHI。

这里写图片描述

荣耀彩票代理SHANGTUXIANSHIDESHISpinnerCHANGJIANDEYANGSHI。ZHEIPIANWENZHANGZHONGWOJIANGTAOLUN1.SpinnerDEJIBENYONGFA 2.SHEZHISpinnerDEAdapter (arrayadapter HEZIDINGYIBaseAdapter)3.SpinnerDECAIDANXIANSHIFANGSHI 4.SpinnerDExmlSHUXING

最简单的Spinner

荣耀彩票代理ZAIBUJUWENJIANZHONGTIANJIASpinnerKONGJIAN

 <LinearLayout 
        android:layout_width='fill_parent'
        android:layout_height='fill_parent'
        android:orientation='vertical' >

        <Spinner
            android:id='@+id/spinner1'
            android:layout_width='wrap_content'
            android:layout_height='wrap_content'
            android:entries='@array/languages'
          />
    </LinearLayout>

荣耀彩票代理QIZHONGandroid:entries=”@array/languages”BIAOSHISpinnerDESHUJUJIHESHICONGZIYUANSHUZUlanguagesZHONGHUOQUDE,languagesSHUZUZIYUANDINGYIZAIvalues/arrays.xmlZHONG:

<?xml version='1.0' encoding='utf-8'?>
<resources>
    <string-array name='languages'>
        <item>c语言</item>
        <item>java </item>
        <item>php</item>
        <item>xml</item>
        <item>html</item>
    </string-array>
</resources>

荣耀彩票代理RUGUONIBUXUYAODUISpinnerDEXUANZESHIJIANZUOXIANGYING,NEIMEYIGEWANZHENGDESpinnerSHIYONGLIUCHENGJIUJIESHULE。

YUNXINGJIEGUO:

这里写图片描述

荣耀彩票代理DANGRAN,YIBANQINGKUANGXIAWOMENSHIXUYAOXIANGYINGSpinnerXUANZESHIJIANDE,KEYITONGGUOOnItemSelectedListenerDEHUIDIAOFANGFASHIXIAN

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Spinner spinner = (Spinner) findViewById(R.id.spinner1);
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, 
                    int pos, long id) {

                String[] languages = getResources().getStringArray(R.array.languages);
                Toast.makeText(MainActivity.this, '你点击的是:'+languages[pos], 2000).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
    }

}

荣耀彩票代理SHANGMIANDESpinnerKANQILAIFEICHANGPIAOLIANG,BUGUOTABINGBUSHIZONGSHIRUCI,GANGGANGKANDAODESHIZAIandroid:Theme.Holo.LightZHUTIXIADEXIAOGUO,TONGYANGDEDAIMARUGUOZAIandroid:Theme.LightXIAMIANJIUHUIBIANDEHENCHOU。

这里写图片描述 这里写图片描述

荣耀彩票代理XIANGBIZHEIYESHIHENDUORENBUXIANGSHIYONGSpinnerDEYUANYINLEBA。RUGUOXIANGJIANRONG2.3,ZEZHINENGRENSHOUZHEIYANGDEXIAOGUO。

设置Spinner的Adapter
上面使用Spinner数据源于xml数组,其实用的最多的还是通过adapter来跟Spinner绑定数据。

使用ArrayAdapter

// 初始化控件
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
// 建立数据源
String[] mItems = getResources().getStringArray(R.array.languages);
// 建立Adapter并且绑定数据源
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, mItems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//绑定 Adapter到控件
spinner .setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, 
            int pos, long id) {

        String[] languages = getResources().getStringArray(R.array.languages);
        Toast.makeText(MainActivity.this, '你点击的是:'+languages[pos], 2000).show();
    }
    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }
});

这是Spinner的标准使用方法,其中,有两行代码可以决定Spinner的外观:

ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, mItems);

DIERGECANSHUSHISpinnerWEIZHANKAICAIDANSHISpinnerDEMORENYANGSHI,android.R.layout.simple_spinner_itemSHIXITONGZIDAIDENEIZHIBUJU。

2

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

荣耀彩票代理SHEZHIDESHIZHANKAIDESHIHOUXIALACAIDANDEYANGSHI(ZHUYIHESHANGMIANQUBIE),TONGLIandroid.R.layout.simple_spinner_dropdown_itemYESHINEIZHIBUJU。

荣耀彩票代理RUGUOBUSHEZHIadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)HUIZENYANGNI?

HUIZAOCHENGWEIZHANKAIDEsipnnerHEZHANKAIDECAIDANDOUSHIYIZHONGBUJUYANGSHI。XIAMIANYIYUNXINGJIETULAISHUOMING:

MEIYOUadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item):

这里写图片描述

荣耀彩票代理YOUsetDropDownViewResource:

这里写图片描述

荣耀彩票代理NIKENENGHUIHENHAOQIRUGUOSHEZHILEsetDropDownViewResourceDANSHIsetDropDownViewResourceDECANSHUHEArrayAdapterDEDIERGEBUJUCANSHU(simple_spinner_item)YIZHIDEQINGKUANGXIAHUIZENYANG。QINGKUANGSHIHEMEIYOUsetDropDownViewResourceSHIYIYANGDE,YINWEISHANGMIANSHUOLEBUSHEZHIsetDropDownViewResource SpinnerWEIZHANKAIHEZHANKAIDOUSHIYONGDEYIYANGDEBUJU。

荣耀彩票代理QISHIsimple_spinner_itemHEsimple_spinner_dropdown_itemLIANGZHEDEMINGZIZHENGHAOFANYINGLETAMENDEQUBIE。YIGEYINGYONGYUXIALAYIGEYINGYONGYUSpinnerBENSHEN。

使用自定义的BaseAdapter

ZHEIZHONGQINGKUANGSHIYONGYUspinnerBIJIAOFUZADEQINGKUANG,BIRUDAIYOUTUBIAO。

XIAMIANWOMENDINGYIYIGEXUANZELIANXIRENDESpinner。

 <LinearLayout 
        android:layout_width='fill_parent'
        android:layout_height='80dip'
        android:orientation='vertical' >

        <Spinner
            android:id='@+id/spinner2'
            android:layout_width='wrap_content'
            android:layout_height='wrap_content'
          />
    </LinearLayout>

activityZHONG:

    // 初始化控件
        Spinner spinner2  = (Spinner) findViewById(R.id.spinner2);
        // 建立数据源
        List<Person>  persons=new ArrayList<Person>();
        persons.add(new Person('张三', '上海 '));
        persons.add(new Person('李四', '上海 '));
        persons.add(new Person('王五', '北京' ));
        persons.add(new Person('赵六', '广州 '));
        //  建立Adapter绑定数据源
        MyAdapter _MyAdapter=new MyAdapter(this, persons);
        //绑定Adapter
        spinner2.setAdapter(_MyAdapter);

Person.java

package com.example.spinnerdemo;

public class Person {
    private String personName;
    private String personAddress;
    public Person(String personName, String personAddress) {
        super();
        this.personName = personName;
        this.personAddress = personAddress;
    }
    public String getPersonName() {
        return personName;
    }
    public void setPersonName(String personName) {
        this.personName = personName;
    }
    public String getPersonAddress() {
        return personAddress;
    }
    public void setPersonAddress(String personAddress) {
        this.personAddress = personAddress;
    }

}

荣耀彩票代理MyAdapter.java

package com.example.spinnerdemo;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 自定义适配器类
 * @author jiangqq  <a href=http://blog.csdn.net/jiangqq781931404></a>
 *
 */
public class MyAdapter extends BaseAdapter {
    private List<Person> mList;
    private Context mContext;

    public MyAdapter(Context pContext, List<Person> pList) {
        this.mContext = pContext;
        this.mList = pList;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    /**
     * 下面是重要代码
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater _LayoutInflater=LayoutInflater.from(mContext);
        convertView=_LayoutInflater.inflate(R.layout.item_custom, null);
        if(convertView!=null) {
            ImageView imageView = (ImageView)convertView.findViewById(R.id.image);
            imageView.setImageResource(R.drawable.ic_launcher);
            TextView _TextView1=(TextView)convertView.findViewById(R.id.textView1);
            TextView _TextView2=(TextView)convertView.findViewById(R.id.textView2);
            _TextView1.setText(mList.get(position).getPersonName());
            _TextView2.setText(mList.get(position).getPersonAddress());
        }
        return convertView;
    }
}

YUNXINGXIAOGUO:

这里写图片描述

Spinner的菜单显示方式

TAYOULIANGZHONGXIANSHIXINGSHI,YIZHONGSHIXIALACAIDAN,YIZHONGSHIDANCHUKUANG,CAIDANXIANSHIXINGSHISHIspinnerModeSHUXINGJUEDINGDE:

android:spinnerMode='dropdown'
android:spinnerMode='dialog'

ZAIandroid2.3SHANGMEIYOUZHEIGESHUXING,XITONGMORENJIANGSpinnerDANCHUCAIDANXIANSHICHENGdialog。XIAMIANSHITheme.LightHETheme.Holo.LightXIASpinnerZAIBUTONGMOSHIXIADEXIAOGUODUIBITU。

这里写图片描述

Spinner的xml属性

荣耀彩票代理QISHISHANGMIANYIJINGTIDAOLESpinnerDELIANGGESHUXING:

荣耀彩票代理1 entries: ZHIJIEZAIxmlBUJUWENJIANZHONGBANGDINGSHUJUYUAN(KEYIBUSHEZHI,JIKEYIZAIActivityZHONGDONGTAIBANGDING)

荣耀彩票代理2 spinnerMode: SpinnerDEXIANSHIXINGSHI

荣耀彩票代理CHUCIZHIWAIHAIYOURUXIASHUXING:

prompt:ZAISpinnerDANCHUXUANZEDUIHUAKUANGDESHIHOUDUIHUAKUANGDEBIAOTI:

这里写图片描述

SHUXINGMINGCHENG

android:dropDownHorizontalOffset

DUIYINGFANGFA:

setDropDownHorizontalOffset(int)

spinnerMode=”dropdown”SHI,XIALADEXIANGMUXUANZECHUANGKOUZAISHUIPINGFANGXIANGXIANGDUIYUSpinnerCHUANGKOUDEPIANYILIANG。

TABIXUSHIYIGEDAIYOUDANWEIDEFUDIANXINGCHICUNZHI,RU:”14.5sp”。YOUXIAODEDANWEIBAOKUO:px(XIANGSU)、dp(MIDUWUGUANDEXIANGSU)、sp(JIYUYINYONGZITIDECHICUNLAISUOFANGDEXIANGSU)、in(YINGCUN)、mm(HAOMI)。

ZHEIGESHUXINGHAIKEYIYINYONGYIGEZIYUAN(GESHI:@[package:]type:name)HUOZHESHIBAOHANZHEIZHONGLEIXINGZHIDEZHUTISHUXING(GESHI:?[package][type:]name)。

ZHEIGESHUXINGDUIYINGQUANJUSHUXINGZIYUANFUHAOdropDownHorizontalOffset。

android:dropDownSelector

YONGYUSHEDINGspinnerMode=”dropdown”SHILIEBIAOXUANZEQIDEXIANSHIXIAOGUO。

TAKEYIYONG”@[+][package]:type:name”GESHILAIYINYONGLINGWAIDEZIYUAN,HUOZHESHIYONG”?[package:][type:]name”DEGESHILAI YINGYONGZHUTISHUXING,HAIKEYISHI”#rgb”、”#argb”、”#rrggbb”、”aarrggbb”GESHIDEYANSEZHI。

TADUIYINGDEQUANJUSHUXINGZIYUANFUHAOSHIdropDownSelector。

android:dropDownVerticalOffset

DUIYINGFANGFA:

setDropDownVerticalOffset(int)

spinnerMode=”dropdown”SHI,XIALADEXIANGMUXUANZECHUANGKOUZAICHUIZHIFANGXIANGXIANGDUIYUSpinnerCHUANGKOUDEPIANYILIANG。

ZHEIGESHUXINGTABIXUSHIYIGEDAIYOUDANWEIDEFUDIANXINGCHICUNZHI,RU:”14.5sp”。YOUXIAODEDANWEIBAOKUO:px(XIANGSU)、dp(MIDUWUGUANDEXIANGSU)、sp(JIYUYINYONGZITIDECHICUNLAISUOFANGDEXIANGSU)、in(YINGCUN)、mm(HAOMI)。

荣耀彩票代理HAIKEYIYINYONGYIGEZIYUAN(GESHI:@[package:]type:name)HUOZHESHIBAOHANZHEIZHONGLEIXINGZHIDEZHUTISHUXING(GESHI:?[package][type:]name)。

ZHEIGESHUXINGDUIYINGQUANJUSHUXINGZIYUANFUHAOdropDownVerticalOffset。

android:dropDownWidth

DUIYINGFANGFA:

setDropDownWidth(int)

ZAIspinnerMode=”dropdown”SHI,SHEDINGXIALAKUANGDEKUANDU。

ZHEIGESHUXINGKEYISHIDAIYOUDANWEIDEFUDIANXINGDECHICUNZHI,RU:14.5sp。YOUXIAODEDANWEIBAOKUO:px(XIANGSU)、dp(MIDUWUGUANDEXIANGSU)、sp(JIYUYINYONGZITIDECHICUNLAISUOFANGDEXIANGSU)、in(YINGCUN)、mm(HAOMI)。

HAIKEYIYINYONGYIGEZIYUAN(GESHI:@[package:]type:name)HUOZHESHIBAOHANZHEIZHONGLEIXINGZHIDEZHUTISHUXING(GESHI:?[package][type:]name)。

HAIKEYISHIXIALIECHANGLIANGZHIYI:

fill_parent = -1,XIALAKUANGDEKUANDUYINGGAISHIYONGPINGMUDEKUANDULAISHEDING。ZHEIGECHANGLIANGCONGAPI Level 8KAISHIBEIFEIQILE,BINGQIESHIYONGmach_parentCHANGLIANGLAIDAITI。

mach_parent = -1,XIALAKUANGDEKUANDUYINGGAISHIYONGPINGMUDEKUANDULAISHEDING。ZAIAPI Level 8ZHONGBEIYINRU。

荣耀彩票代理wrap_content = -2,XIALAKUANGDEKUANDUYINGGAIGENTADENEIRONGXIANGSHIYING。

荣耀彩票代理TADUIYINGDEQUANJUZIYUANFUHAOSHIdropDownWidth。

android:gravity

DUIYINGFANGFA:

1
setGravity(int)
这个属性用于设置当前选择的项目的对齐方式。

荣耀彩票代理TABIXUSHIYIXIACHANGLIANGZHIZHIYIHUOZUHE(YONG”|”FUHAOFENLI)。

荣耀彩票代理top = 0x30:BAXUANZEDEDUIXIANGFANGDAOTADERONGQIDEDINGBU,BUGAIBIANTADECHICUN。

bottom = 0x50:BAXUANZEDEDUIXIANGFANGDAOTADERONGQIDEDIBU,BUGAIBIANTADECHICUN。

left = 0x03:BAXUANZEDEDUIXIANGFANGDAOTADERONGQIDEZUOBIAN,BUGAIBIANTADECHICUN。

right = 0x05:BAXUANZEDEDUIXIANGFANGDAOTADERONGQIDEYOUBIAN,BUGAIBIANTADECHICUN。

荣耀彩票代理center_vertical = 0x10:BAXUANZEDEDUIXIANGFANGDAOTADERONGQIDECHUIZHIZHONGXIN,BUGAIBIANTADECHICUN。

荣耀彩票代理fill_vertical = 0x70:WEILEWANQUANDETIANCHONGTADERONGQI,XITONGHUIGENJUXUYAOLAIZENGJIAXUANZEDUIXIANGDECHUIZHICHICUN。

center_horizontal = 0x01:BAXUANZEDEDUIXIANGFANGDAOTADERONGQIDESHUIPINGZHONGXIN,BUGAIBIANTADECHICUN。

fill_horizontal = 0x07:WEILEWANQUANDETIANCHONGTADERONGQI,XITONGHUIGENJUXUYAOLAIZENGJIAXUANZEDUIXIANGDESHUIPINGCHICUN。

center = 0x11:BAXUANZEDEDUIXIANGFANGDAOTADERONGQIDECHUIZHIHESHUIPINGZHONGXIN,BUGAIBIANTADECHICUN。

荣耀彩票代理fill = 0x77:WEILEWANQUANDETIANCHONGTADERONGQI,XITONGHUIGENJUXUYAOLAIZENGJIAXUANZEDUIXIANGDESHUIPINGHECHUIZHICHICUN。

clip_vertical = 0x80:FUJIADEKEXUANSHEZHI,TAKEYISHEZHIRONGQINEIZIDUIXIANGDESHANGXIABIANYUANCAIJIANTADERONGQIBIANKUANG。CAIJIANHUIJIYUCHUIZHIDUIQIDEFANGSHI:DINGBUDUIQIDEHUICAIJIANDIBUBIANYUAN,DIBUDUIQIDEHUICAIJIANDINGBUBIANYUAN,BUHUISHANGXIABIANYUANDOUCAIJIAN。

荣耀彩票代理clip_horizontal = 0x08:FUJIADEKEXUANSHEZHI,TAKEYISHEZHIRONGQINEIZIDUIXIANGDEZUOYOUBIANYUANCAIJIANTADERONGQIBIANKUANG。CAIJIANHUIJIYUSHUIPINGDUIQIDEFANGSHI:ZUODUIQIDEHUICAIJIANYOUBIANYUAN,YOUDUIQIDEHUICAIJIANZUOBIANYUAN,BUHUIZUOYOUBIANYUANDOUCAIJIAN。

start = 0x00800003:BADUIXIANGFANGDAOTADERONGQIDEKAISHIWEIZHI,BUGAIBIANTADECHICUN。

end = 0x00800005:BADUIXIANGFANGDAOTADERONGQIDEJIESHUWEIZHI,BUGAIBIANTADECHICUN。

DUIYINGDEQUANJUSHUXINGZIYUANFUHAOSHIgravity。

荣耀彩票代理ZHU:SpinnerDUIXIANGSHIYIGESHICHUANGDUIXIANGRONGQI,SHEZHITADEgravitySHUXINGSHI,ZHIHUIGAIBIANRONGQINEIBUZISHICHUANGDUIXIANGDEDUIQIFANGSHI,BINGBUHUIGAIBIANZISHICHUANGNEIBUNEIRONGDEDUIQIFANGSHI。

android:popupBackground

DUIYINGFANGFA:

1
setPopupBackgroundResource(int)
在spinner=”dropdown”时,使用这个属性来设置下拉列表的背景。

KEYISHIYONG”@[+][package:]type:name”GESHILAIYINYONGLINGWAIDEZIYUAN,HUOZHESHIYONG”?[package:][type:]name”GESHILAIYING YONGZHUTISHUXING,YEKEYISHIYONG”#rgb”、”#argb”、”#rrggbb”、”#aarrggbb”GESHIDEYANSEZHI。

DUIYINGDEQUANJUSHUXINGZIYUANFUHAOSHIpopupBackground

延伸阅读:

Tag标签:   
  • 专题推荐

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规