【问题标题】:How to create 2 column layout dynamically in android?如何在android中动态创建2列布局?
【发布时间】:2013-08-03 09:04:41
【问题描述】:

模拟

概述

都是带有按钮ImageView和滚动

要求

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

按下 Button 后,就会检测图像。

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


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

提前谢谢您。

【问题讨论】:

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


【解决方案1】:

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

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 用于动态填充GridView。查看this 用于创建自定义适配器。在CustomAdapter getView() 中扩展布局(在第2 步中)

【讨论】:

    【解决方案2】:

    试试这样的,

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

    【讨论】:

    • 我说过,我要动态列出而不是静态的,它应该是检测按钮点击...再过一遍问题,谢谢你的回复。
    • 对不起。我已经根据您的需要改进了我的 ans。请查看它。
    • 感谢您的关注,我只是检查一下,如果它解决了我的问题,那么我接受您的回答。 :)
    【解决方案3】:

    创建一个以 LinearLayout 作为主布局且方向为 Vertical 的 ScrollView。使用下面的代码中的两个按钮添加线性 laoyut 的您想要支付多少金额

    LinearLayout llay = new LinearLayout(this); 
    llay.setOrientation(LinearLayout.HORIZONTAL); 
    
    LinearLayout.LayoutParams llp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-21
      • 2021-03-24
      • 1970-01-01
      • 2012-01-11
      • 1970-01-01
      • 1970-01-01
      • 2013-12-11
      • 1970-01-01
      相关资源
      最近更新 更多