我是android的新手,我对我现在正在做的项目有疑问。
我正在开发一个应用程序,以3x3格式显示总共9张图像,如下图所示。


当我按下下一步按钮时,图像更改为另外9张图像。如果剩余图像不是9张,则图像将按照剩余数量显示,如下图所示。 (例如,剩下的是6张图片)



问题是:


显示图像的最佳方法是什么?我的想法是创建一个包含9个ImageViews的视图
如果我有2个xml布局,第一个是主布局,第二个是包含ImageViews的布局,如何将第二个插入第一个?
以及如何根据上述说明动态插入图片?



请帮我一些代码。我非常感谢您的帮助。对不起,如果我的英语不太好。

先感谢您。



更新

在这种情况下,我尝试使用GridView,这是我第一次使用GridView,所以我使用了here中的示例并将其实现为我的示例(我尝试了包含在其中的示例,并且可以正常工作)。

但是,我已经检查了很多次,没有错误来自LogCat,没有强制关闭,图像没有显示。我不知道哪里出了问题。

这是我的代码:

选择pic.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/bg_inner">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/book_inner"
            android:layout_marginTop="50dp"
        />

        <ImageButton
            android:id="@+id/homeBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/home_btn"
            android:background="@null"
        />

        <ImageView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:src="@drawable/bg_arrow_btn"
            android:layout_alignParentRight="true"
        />

        <ImageButton
            android:id="@+id/nextBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/right_arrow"
            android:background="@null"
            android:layout_alignParentRight="true"
            android:layout_marginTop="5dp"
            android:layout_marginRight="7dp"
            android:layout_marginLeft="7dp"
        />

        <ImageButton
            android:id="@+id/prevBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/left_arrow"
            android:background="@null"
            android:layout_toLeftOf="@+id/nextBtn"
            android:layout_marginTop="5dp"
        />

        <GridView
            android:id="@+id/gridView1"
            android:numColumns="3"
            android:gravity="center"
            android:columnWidth="30dp"
            android:stretchMode="columnWidth"
            android:layout_width="300dp"
            android:layout_height="200dp"
            android:layout_marginLeft="60dp"
            android:layout_marginTop="70dp"
        >

        </GridView>
</RelativeLayout>


animalbutton.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
    />
</LinearLayout>


ImageAdapter.java

    public class ImageAdapter extends BaseAdapter{

    private Context context;
    private final String[] animalValues;

    public ImageAdapter(Context context, String[] animalValues) {
        this.context = context;
        this.animalValues = animalValues;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {

            gridView = new View(context);

            // get layout from mobile.xml
            gridView = inflater.inflate(R.layout.animalbutton, null);

            // set image based on selected text
            ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image);

            String animal = animalValues[position];

            if (animal.equals("Cat")) {
                imageView.setImageResource(R.drawable.anim_cat);
            } else if (animal.equals("Cow")) {
                imageView.setImageResource(R.drawable.anim_cow);
            } else if (animal.equals("Croc")) {
                imageView.setImageResource(R.drawable.anim_croc);
            } else if(animal.equals("Duck")){
                imageView.setImageResource(R.drawable.anim_duck);
            } else if(animal.equals("Elephant")){
                imageView.setImageResource(R.drawable.anim_elephant);
            } else if(animal.equals("Giraffe")){
                imageView.setImageResource(R.drawable.anim_giraffe);
            } else if(animal.equals("Lion")){
                imageView.setImageResource(R.drawable.anim_lion);
            } else if(animal.equals("Moose")){
                imageView.setImageResource(R.drawable.anim_moose);
            } else if(animal.equals("Mouse")){
                imageView.setImageResource(R.drawable.anim_mouse);
            }else {imageView.setImageResource(R.drawable.ic_launcher);}


        } else {
            gridView = (View) convertView;
        }

        return gridView;
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return animalValues[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
}


选择pic.java

    public class choosepic extends Activity {
    /** Called when the activity is first created. */

    ImageAdapter mAdapter;
    GridView gridView;
    static final String[] animal = new String[] {
        "Cat", "Cow","Croc", "Duck", "Elephant", "Giraffe", "Lion", "Moose", "Mouse"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choosepic);

        mAdapter = new ImageAdapter(this, animal);

        gridView = (GridView) findViewById(R.id.gridView1);
        gridView.setAdapter(mAdapter);

        gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
                Toast.makeText(getApplicationContext(), mAdapter.getItem(position), Toast.LENGTH_SHORT).show();

            }
        });
    }
}


我需要一些帮助。先感谢您!

最佳答案

我已经找到答案了。


我正在使用GridView显示图像。只需更改适配器即可更改内容
这个问题不必回答。
与答案编号相同1.代码类似于我之前发布的choicepic.xml。


以及为什么我的`GridView不显示图像的问题,ImageAdapter中的coz getCount()返回0,所以GridView中没有显示图像。
我用return 0;更改了return animalValues.size()

10-06 13:21
查看更多