Android下拉列表spinner的實(shí)例代碼
spinner組件有點(diǎn)類型于HTML中的下拉框<Select></select>的樣子,讓用戶每次從下拉框中選取一個(gè),本文為大家分享了Android下拉列表spinner的具體實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
mian.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".Main" > <Spinner android:id="@+id/spinner" android:layout_width="300sp" android:layout_height="50sp" android:layout_gravity="center_horizontal" /> </LinearLayout>
Main.java
package com.app.main;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class Main extends Activity {
Spinner spinner = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinner = (Spinner) this.findViewById(R.id.spinner);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, new String[] { "第一項(xiàng)",
"第二項(xiàng)", "第三項(xiàng)" });
//設(shè)置下拉樣式
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(adapter);
}
}
效果圖:

其中主要涉及到兩個(gè)xml文件,一個(gè)是填充數(shù)據(jù)時(shí)的simple_spinner_item.xml和下拉樣式simple_dropdown_item_1line
simple_spinner_item.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" style="?android:attr/spinnerItemStyle" android:singleLine="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:textAlignment="inherit"/>
simple_dropdown_item_1line.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" style="?android:attr/dropDownItemStyle" android:textAppearance="?android:attr/textAppearanceLargePopupMenu" android:singleLine="true" android:layout_width="match_parent" android:layout_height="?android:attr/listPreferredItemHeight" android:ellipsize="marquee" />
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)Android軟件編程有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)聯(lián)動(dòng)下拉框 下拉列表spinner的實(shí)例代碼
- Android UI組件Spinner下拉列表詳解
- Android下拉列表選項(xiàng)框及指示箭頭動(dòng)畫
- Android自定義Spinner下拉列表(使用ArrayAdapter和自定義Adapter實(shí)現(xiàn))
- Android控件Spinner實(shí)現(xiàn)下拉列表及監(jiān)聽功能
- Android仿微信實(shí)現(xiàn)下拉列表
- Android自定義單選多選下拉列表的實(shí)例代碼
- Android仿美團(tuán)淘寶實(shí)現(xiàn)多級(jí)下拉列表菜單功能
- Android使用Spinner控件實(shí)現(xiàn)下拉列表的案例
- Android Studio實(shí)現(xiàn)下拉列表效果
相關(guān)文章
android studio3.0以上如何通過navicat訪問SQLite數(shù)據(jù)庫文件
這篇文章主要介紹了android studio3.0以上如何通過navicat訪問SQLite數(shù)據(jù)庫文件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Android使用viewpager實(shí)現(xiàn)自動(dòng)無限輪播圖
這篇文章主要介紹了Android使用viewpager實(shí)現(xiàn)自動(dòng)無限輪播圖效果,實(shí)現(xiàn)方法大概有兩種,一種是viewpager+作為游標(biāo)的點(diǎn) 。另外一種是重寫viewpager,具體實(shí)現(xiàn)過程大家參考下本文2018-06-06
詳解OpenGL Shader抗鋸齒的實(shí)現(xiàn)
普通繪制圓形形狀時(shí)可以看到圖形邊緣會(huì)有明顯鋸齒現(xiàn)象并不像真實(shí)圓形形狀一樣圓潤邊緣平滑。本文將介紹如何通過自制函數(shù)實(shí)現(xiàn)抗鋸齒,需要的可以參考一下2022-02-02
Android自定義Span實(shí)現(xiàn)文字漸變效果
Android提供一些Span設(shè)置文本樣式外,開發(fā)者若希望實(shí)現(xiàn)一些新特性也是能自定義開發(fā)實(shí)現(xiàn)的。本文就將自定義Span實(shí)現(xiàn)文字漸變效果,感興趣的可以嘗試一下2022-06-06
Android UI控件RatingBar實(shí)現(xiàn)自定義星星評(píng)分效果
這篇文章主要為大家詳細(xì)介紹了Android UI控件RatingBar實(shí)現(xiàn)自定義星星評(píng)分效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
詳解 Android中Libgdx使用ShapeRenderer自定義Actor解決無法接收到Touch事件的問題
這篇文章主要介紹了詳解 Android中Libgdx使用ShapeRenderer自定義Actor解決無法接收到Touch事件的問題的相關(guān)資料,希望通過本文能幫助到大家解決這樣的問題,需要的朋友可以參考下2017-09-09
Android Studio 3.0 原生支持kotlin 例子詳解
這篇文章主要介紹了 Android Studio 3.0 原生支持kotlin 例子詳解,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05
Android穩(wěn)定性:可遠(yuǎn)程配置化的Looper兜底框架
這篇文章主要為大家介紹了Android穩(wěn)定性可遠(yuǎn)程配置化的Looper兜底框架實(shí)例實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android實(shí)現(xiàn)斷點(diǎn)下載的方法
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)斷點(diǎn)下載的方法,感興趣的小伙伴們可以參考一下2016-03-03

