以编程方式在Android中添加布局

4
我正在开发一个应用程序,它将从SD卡中获取图像,并将它们分别显示在布局中,就像这样:enter image description here
The Issue is:

假设用户在SD卡中有100多个文件,为了在布局中显示它们,我需要首先添加100多个视图,然后再进行样式设置。 错误的方法

My Question is:

无论是通过编程还是通过XML,我都可以定义仅五个基本视图,并根据SD卡中的图像数量创建其余布局和视图,以便我可以通过水平滚动视图访问它们。

My research includes:

我尝试使用网格视图布局,但它不能满足我的需求,因为它具有所有网格的统一大小。此外,我知道如何通过编程添加布局,但我不知道如何使其与我的设计配合。


你可以使用GridView或StaggeredGridView,https://github.com/etsy/AndroidStaggeredGrid - Raghunandan
你可以在HorizontalScrollView中使用GridLayout,加载一些文件,并在用户滚动到末尾时加载更多。 - Ayoub
2个回答

1
你可以通过将XML设置为两侧滚动来实现它,方法如下-

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="320px" android:layout_height="fill_parent">

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linlay" android:layout_width="320px"
        android:layout_height="fill_parent" android:stretchColumns="1"
        android:background="#000000"/>

</HorizontalScrollView>


0
使用以下方法:-
1.) 将所有图像的路径放入一个arraylist中。
2.) 创建一个for循环,它将运行到arraylist的大小。
3.) 在此循环中,使用代码创建您的imageview。
ImageView im = new ImageView(this);

4.) 显示图片,将路径从arraylist添加到src属性中。

您需要使用一些额外的布局参数来根据您的需求进行设计。


我需要定义每个图像相对于另一个的相关性..那么我们该如何管理它呢? - Hassaan Rabbani
为此,您需要为不同的属性创建不同的ArrayList,并对于n个ImageView,在x ArrayList中调用与其相关的属性。 - Payal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接