如何在android中动态创建2列布局?

时间:2013-08-02 07:20:56

标签: android button scroll imageview two-column-layout

MOCK UP

2-column layout

概述

所有 ImageView 按钮并滚动

要求

我想为图片显示实现2列修复布局。

按下按钮检测 图片。

如果只有1张图像,那么它只会在布局中显示一张图像。


如果有人知道解决方案,请分享。

提前谢谢。

3 个答案:

答案 0 :(得分:2)

尝试使用GridView。查看this以供参考。对于 2列布局,请android:numColumns = "2"。使用ImageView动态加载ButtonAdapter。 为此,

1.创建一个包含GridView

的xml
<GridView
    android:id="@+id/gridFriends"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="2"/>

2.要定义GridView的内容,请创建另一个XML布局,该布局将包含ImageView和ImageButton。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainContainer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
        <ImageView
            android:id="@+id/img"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            android:src="@null" />

        <Button
            android:id="@+id/btn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center">
        <Button>
   </RelativeLayout>

创建customAdapter以动态填充GridViewthis以创建自定义适配器。CustomAdapter getView()对布局进行充气(在步骤2中) )

答案 1 :(得分:0)

使用LinearLayout创建一个ScrollView作为主要布局,方向为Vertical。使用下面的代码

这样的两个按钮添加线性laoyut的父母支付的数量
LinearLayout llay = new LinearLayout(this); 
llay.setOrientation(LinearLayout.HORIZONTAL); 

LinearLayout.LayoutParams llp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

答案 2 :(得分:0)

尝试这样的事情,

<scrollview>
    <LinearLayout
        android:orientation = "vertical"
        android:height = "match_parent"
        android:width = "match_parent"
    >
      //for two column image view
        <LinearLayout
            android:id = "@+id/image_view_container1"
            android:orientation = "horiZontal"
            android:height = "match_parent"
            android:width = "0dp"
            android:weight = "1">
            <LinearLayout
                android:id = "@+id/image_view_01"
                android:orientation = "vertical"
                android:height = "match_parent"
                android:width = "0dp"
                android:weight = "1">

                <ImageView>
                <Button>
            </LinearLayout>

            <LinearLayout
                android:id = "@+id/image_view_02"
                android:orientation = "vertical"
                android:height = "match_parent"
                android:width = "0dp"
                android:weight = "1">

                <ImageView>
                <Button>
            </LinearLayout>

        </LinearLayout>
    </LinearLayout>
</ScrollView>