我是新來的Android,所以我有一個關於顯示圖像我需要一個代碼,以動態顯示圖像
我已經創造了main.xml
文件按鈕,工作正常,但它顯示的查詢只有一個圖像。我希望所有的圖像能夠一個接一個地顯示,這樣我就可以滑動到左側並檢查左側顯示的可繪製文件夾中的所有圖像。
我已經使用它的代碼是
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<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_width="244dp"
android:layout_height="0dip"
android:layout_weight="0.85"
android:contentDescription="@string/content"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_label" />
</LinearLayout>
mainactivity.java
package com.pokemon.ash;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
ImageView image;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addButtonListener();
}
public void addButtonListener() {
image = (ImageView) findViewById(R.id.image);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//i am using all the image files name
image.setImageResource(R.drawable.blastoise);
image.setImageResource(R.drawable.bulbasaur);
image.setImageResource(R.drawable.charizard);
image.setImageResource(R.drawable.charmander);
image.setImageResource(R.drawable.charmeleon);
image.setImageResource(R.drawable.ivysaur);
image.setImageResource(R.drawable.squirtle);
image.setImageResource(R.drawable.venusaur);
image.setImageResource(R.drawable.wartortle);
//but only last image is printing in the output I want all the images to be taken by drawable folder please help me in this such that i can swipe the images
}
});
}}
這裏只有一個圖像被正確打印我希望所有的圖像可以看出,這樣當我刷到左側,必須轉到其他圖像像水箭龜圖像,並向左滑動它有要去bulbasaur圖像。
您可以使用** ** ViewPager或** ViewFlipper ** 爲了那個原因。 – Piyush
@Sandeep檢查[this](http://manishkpr.webheavens.com/android-viewpager-as-image-slide-gallery-swipe-gallery/)的例子。 – Kunu
可以給你的代碼片段@PiyushGupta –