我是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
}
});
}}
这里只有一张图片正确打印我想要看到所有图像,这样当我向左侧滑动时,它必须转到另一张图像,如 blastoise 图像并向左滑动它必须去到 bulbasaur 图片。
答案 0 :(得分:0)
现在,您的Button会覆盖1个ImageView的ImageResource 9次。这就是为什么你只看到最后一个jpg的原因。一个ImageView只能容纳一个图像。如果要显示9个图像,则必须使用9个ImageViews或使用ViewPager(如已建议的@Piyush Gupta)。此ViewPager的每个页面都有自己的ImageView,其中包含9个图像中的一个。我想你想和ViewPager一起去。有关此内容的更多信息,请访问:http://developer.android.com/training/animation/screen-slide.html
在此链接上,您可以找到有关如何使用与您一样多的页面创建ViewPager的说明 需要:
Create an activity that does the following things: - Sets the content view to be the layout with the ViewPager. - Creates a class that extends the FragmentStatePagerAdapter abstract class and implements the getItem() method to supply instances of ScreenSlidePageFragment as new pages. - The pager adapter also requires that you implement the getCount() method, which returns the amount of pages the adapter will create (five in the example). - Hooks up the PagerAdapter to the ViewPager. - Handles the device's back button by moving backwards in the virtual stack of fragments. If the user is already on the first page, go back on the activity back stack.