Android 应用开发笔记 - 拖动效果(Gallery)
新建一View,清单如下:
view_gallery.xml
[html]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
在面板拖拽图标,然后更改相关属性(Properties),如下:
[html]
<Gallery
android:id="@+id/gallery02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"/>
<Gallery
android:id="@+id/gallery02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"/>
添加一按钮,如下:
[html] v
<Button
android:id="@+id/btnReturn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="166dp"
android:text="@string/btn1Caption" />
<Button
android:id="@+id/btnReturn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="166dp"
android:text="@string/btn1Caption" />
整体布局效果如下:
把“Push me”按钮,更改为切换按钮。即实现切换功能。相关代码请参考多个View切换!
实现Gallery需要继承BaseAdapter,我们命名为ImgAdapter。
代码清单如下:
[java]
package com.example.prjandroid;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class ImgAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return null;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return null;
}
}
package com.example.prjandroid;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class ImgAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return null;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return null;
}
}
如果想要放图片,必须使用ImageView!
这时,我们把图片放到res/drawable下(建议使用png格式文件)。
然后,再新建一xml文件,提供Gallery的背景。如下:
res/attrs.xml
[html]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="galleryThem">
<attr name="android:galleryItemBackground"/>
</declare-styleable>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="galleryThem">
<attr name="android:galleryItemBackground"/>
</declare-styleable>
</resources>
声明一图片ID的数组:
[java]
// resource draw
private int[] resPics = new int[] {
R.drawable.emacs1,
R.drawable.emacs2,
R.drawable.emacs3,
R.drawa
补充:移动开发 , Android ,