Spinner简介

spinner是竖直方向展开一个列表供选择。和gallery都是继承了AbsSpinner,AbsSpinner继承了AdapterView,因此AdaptyerView的属性都可以使用。

Spinner属性

实例

package peng.liu.testview;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity {
    private Spinner spinner02;
    private String[] names = new String[]{
            "java","python","html"
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner02 = (Spinner) findViewById(R.id.spinner02);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,names);
        spinner02.setAdapter(adapter);
    }
}

布局代码

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">
   <Spinner
       android:layout_width="fill_parentw"
       android:layout_height="wrap_content"
       android:id="@+id/spinner01"
       android:entries="@array/books"
       android:prompt="@string/book"
       />
    <Spinner
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner02"
        android:prompt="@string/book"
        />

</LinearLayout>

Gallery简介

Gallery和Spinner都是AbsSpinner的子类,所以它和Spinner相似,但它是水平的展示,而且可以用手拖动查看。但是它在新版本已经不被推荐,代替是HorizontalScrollView和ViewPager。

Gallery属性

图片浏览器实例

package peng.liu.testview;

import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private Gallery gallery;
    private ImageView image;
    private int[] names = new int[]{
       R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gallery = (Gallery) findViewById(R.id.gallery);
        image = (ImageView) findViewById(R.id.image);
        BaseAdapter adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return names.length;
            }

            @Override
            public Object getItem(int i) {
                return null;
            }

            @Override
            public long getItemId(int i) {
                return 0;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                ImageView image = new ImageView(MainActivity.this);
                image.setImageResource(names[i]);
                image.setLayoutParams(new Gallery.LayoutParams(75,100));
                image.setScaleType(ImageView.ScaleType.FIT_XY);
                return image;
            }
        };
        gallery.setAdapter(adapter);
        gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                image.setImageResource(names[i]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }
}

布局代码

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">
   <ImageView
       android:layout_width="320dp"
       android:layout_height="320dp"
       android:id="@+id/image"
       />
    <Gallery
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:unselectedAlpha="0.6"
        android:spacing="2pt"
        android:id="@+id/gallery"/>
</LinearLayout>

Android之Gallery和Spinner-Android学习之旅(二十九)的更多相关文章

  1. Android的RadioButton和checkBox的用法-android学习之旅(十九)

    RadioButton和checkBox简介 单选按钮(RadioButton)和复选框(CheckBox)都继承了Button,因此属性的设置和Button差不多,只是加了一个android:che ...

  2. Dynamic CRM 2013学习笔记(二十九)报表设计:reporting service 报表开发常见问题

    在报表开发过程中,经常会遇到各种各样的问题,比如The report cannot be displayed. (rsProcessingAborted),一点有意义的提示都没有:再就是分页问题,经常 ...

  3. 【Java学习笔记之二十九】Java中的"equals"和"=="的用法及区别

    Java中的"equals"和"=="的用法及区别 在初学Java时,可能会经常碰到下面的代码: String str1 = new String(" ...

  4. HTML5学习笔记(二十九):Cookie和Session

    HTTP协议本身是无状态的,这和HTTP最初的设计是相符的,每次请求都是创建一个短连接,发送请求,得到数据后就关闭连接.即每次连接都是独立的一次连接. 这样的话,导致的问题就是当我在一个页面登陆了账号 ...

  5. Python学习之旅(十九)

    Python基础知识(18):面向对象高级编程(Ⅰ) 使用__slots__:限制实例的属性,只允许实例对类添加某些属性 (1)实例可以随意添加属性 (2)某个实例绑定的方法对另一个实例不起作用 (3 ...

  6. Unity3D学习笔记(二十九):AssetBundle

    AssetBundle 什么是AssetBundle? AssetBundle是把一些资源文件或场景文件,以某种方式保存在一个文件中.一个AssetBundle可以包含模型.材质.图片或场景等.但是A ...

  7. python学习笔记(二十九)为什么python的多线程不能利用多核CPU

    问题:为什么python的多线程不能利用多核CPU,但是咱们在写代码的时候,多线程的确是在并发,而且还比单线程快原因:因为GIL,python只有一个GIL,运行python时,就要拿到这个锁才能执行 ...

  8. Python学习笔记(二十九)ThreadLocal

    import threading #创建全局ThreadLocal对象: local_school = threading.local() def process_student(): #获取当前线程 ...

  9. vue2.x学习笔记(二十九)

    接着前面的内容:https://www.cnblogs.com/yanggb/p/12682822.html. 路由 官方路由 对于大多数的单页面应用,都推荐使用官方支持的vue-router库. 从 ...

随机推荐

  1. mooc-python语言语法week3-6

    week3 1.类型的概念:程序编程不允许有歧义的数据类型存在,所以对数据进行了划分,python语言类型分为,数字类型.字符串类型.元组类型.列表类型.文件类型.字典类型. i:数字类型: pyth ...

  2. C语言程序设计第二次作业—————顺序结构改

    1.输出带框文字:在屏幕上输出以下3行信息. ************* Welcome ************* 源程序 #include <stido.h> int mian() { ...

  3. java随机生成字符串和校验

    首先定义字符串 String a = "0123456789"; // 数字 String b = "abcdefghijklmnopqrstuvwxyz"; ...

  4. dev gridcontrol 无法编辑 解决方案

    1.确认表格打开编辑 gridView1.OptionsBehavior.Editable = True 2.确认列打开编辑 gridView1.Columns("Name").O ...

  5. Python3玩转儿 机器学习(4)

      jupyternotebook 的使用方法¶   最基本的使用¶ In [1]: 1+2 Out[1]: 3   菜单树¶   File¶ |------> New Notebook --- ...

  6. Linux文件基本操作

    TIP:Tab键可以自动补全命令 首先要了解Linux树形结构 1./- 根每一个文件和目录从根目录开始.只有root用户具有该目录下的写权限.请注意,/root是root用户的主目录,这与/.不一样 ...

  7. JavaScript正则表达式模式匹配(3)——贪婪模式和惰性模式

    var pattern=/[a-z]+/; //这里使用了贪婪模式, var str='abcdefg'; alert(str.replace(pattern,'1')); //所有的字符串变成了1 ...

  8. ubuntu 命令行下格式化U盘,磁盘分区

    命令行格式化磁盘一般是:先卸载,后格式化. 先说格式化U盘的方法,格式化磁盘某个分区是同样的道理. 一般情况下U盘会挂载在/meida/<username>/<disk>目录下 ...

  9. 安装oracle时修改Linux版本问题

    Linux安装Oracle报Checking operating system version must be redhat, SuSE, redhat, UnitedLinux or asianux ...

  10. Oracle中表字段相关操作举例

    --创建测试表 create or replace table student ( xh ), --学号 xm ), --姓名 sex ), --性别 birthday date, --日期 sal ...