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>