【问题标题】:How to do RelativeLayout with images like gridview?如何使用gridview之类的图像进行RelativeLayout?
【发布时间】:2015-06-20 09:34:30
【问题描述】:

我需要在滚动视图中显示像 gridview 这样的图像。 据我所知,在滚动视图中使用 gridview 是不对的。 但是我需要大图像然后是文本,然后在一个片段中列出所有图像和所有图像,所以我需要滚动视图,就像第一个一样,但是只是没有任何文字的图像 那么有没有办法在RelativeLayout(如gridview)中以编程方式一个接一个地显示图像?

这是我得到的,但它不起作用,图像只是相互重叠。

 for (String pic : mFlat.getAdv_pics()){
            i++;
            ImageView imageView = new ImageView(mActivity);
            imageView.setId(i);

            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            params.addRule(RelativeLayout.RIGHT_OF, i-1);

            imageView.setLayoutParams(params);

            Log.d("!!!!", "" + i + "|" + imageView.getId());

            Log.d("!!!!", "http://****" + pic + "_small.jpg");
            relativeLayout.addView(imageView);
            Picasso.with(mActivity)
                    .load("http://****" + pic + "_small.jpg")
                    .resize(width,width)
                    .centerCrop()
                    .into(imageView);
        }
    }

谢谢

【问题讨论】:

  • 但是为什么你不想使用Gridview呢? GridView 默认提供滚动机制。
  • 使用定义为代表您专门处理这些复杂性的网格视图
  • 据我所知,在滚动视图中使用 gridview - 是个大问题

标签: android android-layout gridview android-relativelayout


【解决方案1】:

gridview 存在是有原因的,创建这样的布局要复杂得多,使用其他视图组合也更难处理(事件、位置..)。您基本上需要在 realtivelayout 中显示一个列表视图,并且您的列表视图的项目将是您的自定义网格的行。 例如,您想在每行显示 3 张图像:

您的主要 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">

    <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listView"/>
</RelativeLayout>

然后您的列表视图适配器项将如下所示:

<?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">

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView1"/>
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView2"/>

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView3"/>

</RelativeLayout>

现在您需要将一个嵌套数组传递给您的适配器,数组的每个项目包含 3 个图像等等。但主要问题是处理事件,这需要您重新考虑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多