简体   繁体   English

如何制作4x4的imageviews网格?

[英]How do i make a 4x4 grid of imageviews?

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

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:src="@drawable/music" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:src="@drawable/music" />

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:src="@drawable/music" />

<ImageView
    android:id="@+id/imageView4"
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:src="@drawable/music" />

</TableLayout>

This is the original XML, i just need to add more. 这是原始的XML,我只需要添加更多即可。 i have an imageView that's big , so it needs to be shrunk down and copied 16 times into a 4x4 grid. 我有一个很大的imageView,因此需要缩小并将其复制到4x4网格中16次。 I can only get it to go 4 images in one column 我只能在一列中显示4张图像

I don't understand the problem with the large image, but I will tell you my suggestion: 我不了解大图像的问题,但我会告诉您我的建议:

there are multiple possible solutions: 有多种可能的解决方案:

  1. Since you have 16 imageViews that you wish to create, you can use a GridView together with a BaseAdapter . 由于您要创建16个imageView ,因此可以将GridViewBaseAdapter一起使用。 If it's important for you to see it in the xml, use isInEditMode for a custo GridView, and set the adapter there to be your adapter with fake items. 如果对在xml中看到它很重要,请使用isInEditMode作为custo GridView,然后将适配器设置为带有假项目的适配器。 You should be aware of problems with the sizes of the columns/rows on the gridView , especially when changing orientations. 您应该意识到gridView上列/行的大小存在问题,尤其是在更改方向时。

  2. Another alternative could be the GridLayout 另一种选择是GridLayout

  3. If you insist on using the TableLayout, you can have 4 TableRow instances, each has a weight of 1 . 如果您坚持使用TableLayout,则可以有4个TableRow实例,每个实例的权重为1。 in each of them , add 4 imageViews and there each has a weight of 1. 在每个视图中,添加4个imageViews,每个视图的权重为1。

Add 4 TableRow 's and put your ImageView 's into them. 添加4个TableRow并将您的ImageView放入其中。

Or you can create this grid from code. 或者,您可以通过代码创建此网格。 Like this: 像这样:

LinearLayout container = null;
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1);
for (int i = 0; i < 16; ++i) {
    if (i % 4 == 0) {
        container = new LinearLayout(getActivity());
        container.setOrientation(LinearLayout.HORIZONTAL);
        mGrid.addView(container);
    }
    LinearLayout view = (LinearLayout) mLayoutInflater
        .inflate(R.layout.view_item, container, false);
        //populate the view in loop
        view.setLayoutParams(params);
        container.addView(view);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM