(4.1.9)gallery
来源:互联网 发布:tcpip网络层协议有哪些 编辑:程序博客网 时间:2024/06/07 20:47
运行效果如下:
选择一张好看的后,效果如下
activity_main.xml 代码:
[html] view plaincopy
- <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" >
- <ImageButton
- android:id="@+id/myimagebutton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/ic_launcher"
- />
- <EditText
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
弹框的View布局文件代码为:
[html] view plaincopy
- <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"
- >
- <Gallery
- android:id="@+id/mygallery"
- android:layout_width="220dp"
- android:layout_height="60dp"
- android:spacing="10dp" />
- </LinearLayout>
Gallery的item布局文件为:
[html] view plaincopy
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <ImageView
- android:id="@+id/image"
- android:layout_gravity="center_horizontal"
- android:layout_width="40dp"
- android:layout_height="40dp"/>
- <TextView
- android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:textSize="8dp"
- android:gravity="center"
- android:textColor="#ffffffff"/>
- </LinearLayout>
Activity代码为:
[java] view plaincopy
- public class MainActivity extends Activity implements OnItemSelectedListener{
- Gallery gallery;
- int [] smalldrawable = new int[]{R.drawable.sample_thumb_0,R.drawable.sample_thumb_1,R.drawable.sample_thumb_2,R.drawable.sample_thumb_3,R.drawable.sample_thumb_4,R.drawable.sample_thumb_5,R.drawable.sample_thumb_6,R.drawable.sample_thumb_7};
- int position = 0;
- ImageButton imageButton;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- LayoutInflater layoutInflater = LayoutInflater.from(this);
- final View view = layoutInflater.inflate(R.layout.mydialog, null);
- builder.setTitle("请选择一张好看的");
- builder.setView(view);
- gallery = (Gallery) view.findViewById(R.id.mygallery); //此处的findViewById必须要加上view.
- gallery.setAdapter(new ImageAdapter(this));
- gallery.setSelection(0);
- gallery.setOnItemSelectedListener(this);
- imageButton = (ImageButton) findViewById(R.id.myimagebutton);
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- System.out.println(position);
- imageButton.setImageResource(smalldrawable[position]);
- }
- });
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- }
- });
- builder.create().show();
- }
- private class ImageAdapter extends BaseAdapter{
- Context context;
- String [] str = new String[]{"第一张","第二张","第三张","第四张","第五张","第六张","第七张","第八张"};
- public ImageAdapter(Context context) {
- this.context = context;
- }
- //返回图片的个数
- @Override
- public int getCount() {
- return smalldrawable.length;
- }
- @Override
- public Object getItem(int position) {
- return smalldrawable[position];
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder;
- if(convertView == null){
- //View的findViewById()方法也是比较耗时的,因此需要考虑中调用一次,之后用
- //View的getTag()来获取这个ViewHolder对象
- holder = new ViewHolder();
- convertView = View.inflate(context, R.layout.mydialogtype, null);
- holder.imageView = (ImageView) convertView.findViewById(R.id.image);
- holder.textView = (TextView) convertView.findViewById(R.id.text);
- convertView.setTag(holder);
- }else {
- holder = (ViewHolder) convertView.getTag();
- }
- holder.imageView.setImageResource(smalldrawable[position]);
- holder.textView.setText(str[position]);
- return convertView;
- }
- }
- //增加这样一个静态类,缓存一下,这样不用每次都重新加载
- final class ViewHolder{
- public ImageView imageView;
- public TextView textView;
- }
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position,
- long id) {
- int x = 0;
- System.out.println(position);
- this.position = position;
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- }
- }
ok,就这些了。
0 0
- (4.1.9)gallery
- Gallery
- Gallery
- Gallery
- gallery
- gallery
- Gallery
- gallery
- Gallery
- Gallery
- Gallery
- Gallery
- Gallery
- Gallery
- gallery
- gallery
- Gallery
- Gallery
- 安卓基础(十三)
- C# 遍历treeview
- sql server 获取当前时间
- 一带一路倒逼中国外交决策改革
- LeetCode: Find Peak Element
- (4.1.9)gallery
- 知乎:如何评价2010年柴静采访丁仲礼的视频?
- CentOS 6.4下编译安装MySQL 5.6
- app 手机网页一些小知识
- HDU 4499 Cannon
- 操作系统之内存管理2
- LeetCode Maximum Subarray
- viewpager和fragment结合实现碎片切换,改变字体颜色
- SDN你必须知道的十大问题——值得关注的SDN博客