android:dropDownWidth         下拉列表宽度
android:dropDownHorizontalOffset   下拉列表距离左边的距离
android:dropDownVerticalOffset       下拉菜单和文本之间的垂直偏移量
android:popupBackground         下拉列表中的背景色

android:prompt              下拉列表中的提示信息(基本没用了)

MainActivity.java

package com.kale.spinner;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast; public class MainActivity extends Activity { Spinner spinner01,spinner02,spinner03;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewInit();
String []arr = {"JAVA","Android","xml","ios"}; //采用android提供的布局来创建
ArrayAdapter<String> adapter01 = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_multiple_choice,arr);
spinner01.setAdapter(adapter01);
//监听器
spinner01.setOnItemSelectedListener(new OnItemSelectedListener() { @Override
public void onItemSelected(AdapterView<?> adapterView, View arg1,
int position, long id) {
// TODO 自动生成的方法存根
String selected = adapterView.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(), selected, 0).show();
} @Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}); //------------------------------------------------------------------------------ //默认的布局文件设置spinner的样式,用写死的数据来创建的。不是很推荐
ArrayAdapter<CharSequence> adapter02 = ArrayAdapter.createFromResource(this,
R.array.item, android.R.layout.simple_spinner_item);
//设置spinner点开后下拉菜单的样式
adapter02.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner02.setAdapter(adapter02);
//spinner02.setPrompt("test");//设置提示文字,现在的界面中已经不显示了。所以可以说是没用了 //------------------------------------------------------------------------------
//采用动态的数据,布局文件是自己定义的item
ArrayAdapter<String> adapter03 = new ArrayAdapter<>(this, R.layout.item,
R.id.textView_id, arr);
spinner03.setAdapter(adapter03);
}
private void viewInit() {
spinner01 = (Spinner) findViewById(R.id.spinner01_id);
spinner02 = (Spinner) findViewById(R.id.spinner02_id);
spinner03 = (Spinner) findViewById(R.id.spinner03_id);
}
}

activity_main.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"
android:padding="16dp"> <!--
android:dropDownWidth 下拉列表宽度
android:dropDownHorizontalOffset 下拉列表距离左边的距离
android:dropDownVerticalOffset 下拉菜单和文本之间的垂直偏移量
android:popupBackground 下拉列表中的背景色
android:prompt 下拉列表中的提示信息 --> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用代码填充数据的spinner(用android自带的布局)"
android:layout_marginTop="16dp"
android:textAppearance="?android:attr/textAppearanceMedium" /> <Spinner
android:id="@+id/spinner01_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:popupBackground="#ade9ee"
android:dropDownVerticalOffset="3dp"
android:prompt="@string/hello_world"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="数据写到array中的spinner(用android提供的布局)"
android:layout_marginTop="26dp"
android:textAppearance="?android:attr/textAppearanceMedium" /> <Spinner
android:id="@+id/spinner02_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:dropDownVerticalOffset="3dp"
android:prompt="@string/hello_world"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用自定义布局并动态填入数据的spinner"
android:layout_marginTop="26dp"
android:textAppearance="?android:attr/textAppearanceMedium" /> <Spinner
android:id="@+id/spinner03_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> </LinearLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp"> <TextView
android:id="@+id/textView_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textColor="#0000ff"
android:gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout>

用ArrayAdapter来创建Spinner(自定义布局、默认布局、动态内容、静态内容)的更多相关文章

  1. 自定义 Collection View 布局

    自定义 Collection View 布局 answer-huang 29 Mar 2014 分享文章 UICollectionView 在 iOS6 中第一次被引入,也是 UIKit 视图类中的一 ...

  2. iOS系列译文:自定义Collection View布局

    原文出处: Ole Begemann   译文出处: 黄爱武(@answer-huang).欢迎加入技术翻译小组. UICollectionView在iOS6中第一次被介绍,也是UIKit视图类中的一 ...

  3. Customize the View Items Layout 自定义视图项目布局

    In this lesson, you will learn how to customize the default editor layout in a Detail View. For this ...

  4. 自定义Collection View布局

    转自answer-huang的博客 原文出自:Custom Collection View Layouts    UICollectionView在iOS6中第一次被介绍,也是UIKit视图类中的一颗 ...

  5. Android动画效果之自定义ViewGroup添加布局动画

    前言: 前面几篇文章介绍了补间动画.逐帧动画.属性动画,大部分都是针对View来实现的动画,那么该如何为了一个ViewGroup添加动画呢?今天结合自定义ViewGroup来学习一下布局动画.本文将通 ...

  6. 整理 PHPstorm实用个人配置,修改调整个性化快捷键,修改使用phpstorm创建的模板的默认注释:

    对你有助请点赞,请顶------送人玫瑰,手留余香! 1:58 2016/3/12 整理PHPstorm实用个人配置,修改调整个性化快捷键,修改使用phpstorm创建的模板的默认注释: PHPsto ...

  7. Android自定义Dialog及其布局

     实际项目开发中默认的Dialog样式无法满足需求,需要自定义Dialog及其布局,并响应布局中控件的事件. 上效果图: 自定义Dialog,LogoutDialog: 要将自定义布局传入构造函数中, ...

  8. PS网页设计教程XXVI——如何在PS中创建一个专业的网页布局

    作为编码者,美工基础是偏弱的.我们可以参考一些成熟的网页PS教程,提高自身的设计能力.套用一句话,“熟读唐诗三百首,不会作诗也会吟”. 本系列的教程来源于网上的PS教程,都是国外的,全英文的.本人尝试 ...

  9. 如何创建一个自定义jQuery插件

    简介 jQuery 库是专为加快 JavaScript 开发速度而设计的.通过简化编写 JavaScript 的方式,减少代码量.使用 jQuery 库时,您可能会发现您经常为一些常用函数重写相同的代 ...

随机推荐

  1. poj 3468 线段树 成段增减 区间求和

    题意:Q是询问区间和,C是在区间内每个节点加上一个值 Sample Input 10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4Sample O ...

  2. mysql 索引理解

    数据的查询,都需要将数据从磁盘中加载到内存中进行运算加载,索引的出现,让原来每个数据块做一次IO减少为区间范围的快速定位,来减少块的io次数. 如上图,是一颗b+树,关于b+树的定义可以参见B+树,这 ...

  3. CDM中添加Hive服务时Gateway是什么?

    参考这里http://grokbase.com/t/cloudera/scm-users/12aayq5cyh/what-is-gateway-in-cloudera-manager 实际上Gatew ...

  4. MySQL子查询,派生表和通用表达式

    一:子查询 1.介绍 在另一个查询(外部查询)中嵌套另一个查询语句(内部查询),并使用内部查询的结果值作为外部查询条件. 2.子查询在where中 SELECT customerNumber, che ...

  5. 018.Zabbix维护时间和模板导入

    一 维护时间 在某些正常业务维护期间,不需要进行告警,可添加维护时间. 二 维护时间添加 2.1 维护 参数 描述 Name 维护名称 Maintenance type 两种维护类型可选: With ...

  6. java 记录对象前后修改的内容(工具类)

    有时候业务需要,需记录一条记录的修改历史,但是不能为完成任务而硬编码,不靠谱 这种情况可以使用java反射来完成 对对象属性的描述可以通过自定义注解来完成,读取里面的属性进而记录修改历史. 在对象的属 ...

  7. Shape流动效果

    <Window x:Class="MvvmLight1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/ ...

  8. KVM源代码框架

    自己通过看代码总结的内核中包含kvm的文件夹: (1)Linux-3.17.4\Documentation\virtual\kvm\ (2)Linux-3.17.4\include\ (3)Linux ...

  9. 使用httpclient需要的maven依赖

    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore --> <dependency& ...

  10. 使用 IntraWeb (10) - CSS

    IW 会把大多数的视觉属性转换为 CSS; 我们主动使用 CSS 要分两步: 第一步: {通过窗体的 StyleSheet 属性指定要链接的 CSS 文件} procedure TIWForm1.IW ...