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. 使用PL/SQL Developer连接远程DB(本机不安装Oracle客户端)

    本文内容亲测可行环境:    服务端:centos6.7 ,oracle 11g r2 ,动态注册,监听端口号:1521    用户端:win7 ,没有安装ORACLE及其客户端 准备文件:plsql ...

  2. final、finally与finalize的区别

    1. final  在java中,final可以用来修饰类,方法和变量(成员变量或局部变量).下面将对其详细介绍. 1.1 修饰类 当用final修饰类的时,表明该类不能被其他类所继承.当我们需要让一 ...

  3. SpringBoot 中 get/post 请求处理方式,以及requestboy为Json时的处理

    GET.POST方式提时, 根据request header Content-Type的值来判断: application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的 ...

  4. java如何获得数据库表中各字段的字段名

    public class TestDemo { public static Connection getConnection() { Connection conn = null; try { Cla ...

  5. WSGI及gunicorn指北(一)

    作为一个Python Web 开发工程师,pyg0每天都喜滋滋的写着基于各种web框架的业务代码. 突然有一天,技术老大过来跟pyg0说,嘿,我们要新上线一个服务,你来帮我部署一下吧.不用太复杂.用g ...

  6. 智能指针之 unique_ptr

    对于动态申请的内存,C++语言为我们提供了new和delete运算符, 而没有像java一样,提供一个完整的GC机制,因此对于我们申请的动态内存, 我们需要时刻记得释放,且不能重复释放,释放后不能再去 ...

  7. Vue2.0搭建脚手架流程

    介绍 Vue.js是一套构建用户界面的渐进式框架.Vue 只关注视图层,采用自底向上增量开发的设计.Vue 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件. 安装node.js ...

  8. Page2

    css样式表嵌入网页的4种方法: 定义标记的style属性 <标记 style="样式属性:属性值:..;"> 嵌入外部样式表 <style type=" ...

  9. 如何恢复Initial commit之前的源文件

    在github新建了一个空的库,然后到本地文件夹下,git init了一下,将remote和本地的关联起来了,然后git pull了一下,本地的项目源码全没了,用以下命令可以帮你恢复 git refl ...

  10. Node.js 系统

    稳定性: 4 - API 冻结 提供一些基本的操作系统相关函数. 使用 require('os') 访问这个模块. os.tmpdir() 返回操作系统的默认临时文件夹 os.endianness() ...