【发布时间】:2011-11-10 06:36:52
【问题描述】:
我有三个按钮 Image , Videos , Audios 。我想在单击相应按钮时使用音频、视频、图像资源更改图库中的视图,以便只有按钮的相关图像出现在图库中。
我的代码在这里:
XML 布局:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Audios" >
</Button>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Videos" >
</Button>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Images" >
</Button>
</LinearLayout>
<Gallery
android:id="@+id/Gallery01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</Gallery>
</LinearLayout>
<ImageView
android:id="@+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ImageView>
Java 文件:
public class GalleryView extends Activity {
Integer[] pics = {
R.drawable.cloudy,
R.drawable.hazy,
R.drawable.mostlycloudyday,
R.drawable.partlycloud,
R.drawable.sunny,
R.drawable.sunrain,
R.drawable.thunderstorm,
R.drawable.weathercloudy,
};
ImageView imageView;
Button mAudio;
Button mVideo;
Button mImages;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mAudio=(Button) findViewById(R.id.button1);
Gallery ga = (Gallery)findViewById(R.id.Gallery01);
ga.setAdapter(new ImageAdapter(this));
imageView = (ImageView)findViewById(R.id.ImageView01);
ga.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
Toast.makeText(getBaseContext(),
"You have selected picture " + (pos+1) + "of Gallery",
Toast.LENGTH_SHORT).show();
imageView.setImageResource(pics[pos]);
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context ctx;
int imageBackground;
public ImageAdapter(Context c) {
ctx = c;
TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
ta.recycle();
}
@Override
public int getCount() {
return pics.length;
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView iv = new ImageView(ctx);
iv.setImageResource(pics[arg0]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(150,120));
iv.setBackgroundResource(imageBackground);
return iv;
}
}
}
请大家帮帮我,
谢谢。
【问题讨论】:
-
请清楚,您想在图库中显示下一张图片吗?或者想要更改图库中的整个图像?
-
感谢您的评论 Arslan,我希望通过单击按钮来更改整个画廊。
-
请检查我的回答。谢谢
-
谢谢 Arslan。我会接受你的回答,但在 OnItemClick() 函数中,我想为每张点击的图片设置背景,我将如何根据你的代码进行设置?
标签: android button android-gallery