以编程方式添加布局android

时间:2014-01-29 11:02:44

标签: android xml android-layout

我正在开发一个可以从SD卡获取图像的应用程序,并将分别在这样的布局中显示它们

enter image description here

The Issue is:

让我们说用户在SD卡中有100多个文件,要在布局中显示我需要添加前100个视图,然后我会做样式。 Wrong approach

My Question is:

可以通过progamatically或通过XML做什么,以便我只能定义五个基本视图,并根据SD卡中的图像数量,它创建剩余的布局和视图,以便我可以通过水平滚动访问它们图。

My research includes:

我尝试使用网格视图布局,但它没有完全满足我的需求,因为它具有所有网格的统一大小。更多我知道如何以编程方式添加布局,但我不知道如何使它与我的设计一起工作。

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

使用这种方法: -

1。)将路径放在arraylist中的所有图像。

2。)创建一个直到arraylist大小的for循环。

3.)在此循环中,使用代码创建您的imageview。

ImageView im = new ImageView(this);

4。)要显示图像,在src属性中添加arraylist的路径。

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