用ArrayAdapter来创建Spinner(自定义布局、默认布局、动态内容、静态内容)


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(自定义布局、默认布局、动态内容、静态内容)的更多相关文章
- 自定义 Collection View 布局
自定义 Collection View 布局 answer-huang 29 Mar 2014 分享文章 UICollectionView 在 iOS6 中第一次被引入,也是 UIKit 视图类中的一 ...
- iOS系列译文:自定义Collection View布局
原文出处: Ole Begemann 译文出处: 黄爱武(@answer-huang).欢迎加入技术翻译小组. UICollectionView在iOS6中第一次被介绍,也是UIKit视图类中的一 ...
- Customize the View Items Layout 自定义视图项目布局
In this lesson, you will learn how to customize the default editor layout in a Detail View. For this ...
- 自定义Collection View布局
转自answer-huang的博客 原文出自:Custom Collection View Layouts UICollectionView在iOS6中第一次被介绍,也是UIKit视图类中的一颗 ...
- Android动画效果之自定义ViewGroup添加布局动画
前言: 前面几篇文章介绍了补间动画.逐帧动画.属性动画,大部分都是针对View来实现的动画,那么该如何为了一个ViewGroup添加动画呢?今天结合自定义ViewGroup来学习一下布局动画.本文将通 ...
- 整理 PHPstorm实用个人配置,修改调整个性化快捷键,修改使用phpstorm创建的模板的默认注释:
对你有助请点赞,请顶------送人玫瑰,手留余香! 1:58 2016/3/12 整理PHPstorm实用个人配置,修改调整个性化快捷键,修改使用phpstorm创建的模板的默认注释: PHPsto ...
- Android自定义Dialog及其布局
实际项目开发中默认的Dialog样式无法满足需求,需要自定义Dialog及其布局,并响应布局中控件的事件. 上效果图: 自定义Dialog,LogoutDialog: 要将自定义布局传入构造函数中, ...
- PS网页设计教程XXVI——如何在PS中创建一个专业的网页布局
作为编码者,美工基础是偏弱的.我们可以参考一些成熟的网页PS教程,提高自身的设计能力.套用一句话,“熟读唐诗三百首,不会作诗也会吟”. 本系列的教程来源于网上的PS教程,都是国外的,全英文的.本人尝试 ...
- 如何创建一个自定义jQuery插件
简介 jQuery 库是专为加快 JavaScript 开发速度而设计的.通过简化编写 JavaScript 的方式,减少代码量.使用 jQuery 库时,您可能会发现您经常为一些常用函数重写相同的代 ...
随机推荐
- Codeforces Round #440 (Div. 1, based on Technocup 2018 Elimination Round 2) C - Points, Lines and Ready-made Titles
C - Points, Lines and Ready-made Titles 把行列看成是图上的点, 一个点(x, y)就相当于x行 向 y列建立一条边, 我们能得出如果一个联通块是一棵树方案数是2 ...
- 自主搭建CNN训练时遇到的问题
1.训练太慢 用nimibatch代替fullbatch https://www.cnblogs.com/guoyaohua/p/8724433.html 2.过拟合 最直接的解决过拟合问题的办法是增 ...
- 设计模式-装饰者模式(Decorator Pattern)
本文由@呆代待殆原创,转载请注明出处. 此设计模式遵循的设计原则之一:类应该支持扩展,而拒绝修改(Open-Closed Principle) 装饰者模式简述 装饰者模式通过组合的方式扩展对象的特性, ...
- mongodb for windows安装
1,下载mongodb for windwos 下载地址:https://www.mongodb.com/download-center#community 2,创建db和log的文件夹 D:\dat ...
- Wireshark数据抓包教程之安装Wireshark
Wireshark数据抓包教程之安装Wireshark 安装Wireshark 通过上一节的学习可以根据自己的操作系统来下载安装Wireshark了.本书中已开发版1.99.7(中文版)为主,下面介绍 ...
- Linux学习路线+资源
Linux学习路线,个人收集分享 学习路线图 资源链接(蓝色下划线字体对应相应资源链接) Linux 基础 Linux 基础 Linux安装专题教程 Linux中文环境 Linux—从菜鸟到高手 鸟哥 ...
- BZOJ.2521.[SHOI2010]最小生成树(最小割ISAP/Dinic)
题目链接 一条边不变其它边减少可以看做一条边增加其它边不变. 假设要加的边lab为(A->B,v),那么肯定是要使除这条边外,A->B的每条路径上的最小权值都\(>v\),这样在连通 ...
- java基础学习总结——super关键字
一.super关键字
- letter-spacing造成文字无法居中的问题
在使用letter-spacing增加字体间距时,发现字体间距被扩大的同时,字体无法完全居中在div中,如下: 原因:letter-spacing是在字中间产生的间隔,第一个字旁边没有间隔,所以导致不 ...
- UVALive 6889 City Park 并查集
City Park 题目连接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=122283#problem/F Description P ...