【问题标题】:Table layout with 2x2 equal imageview具有 2x2 相等图像视图的表格布局
【发布时间】:2016-06-18 15:04:10
【问题描述】:

我正在尝试创建一个包含文本字段(位于屏幕顶部)的布局,在它下方,我需要以 2x2 的表格形式显示图像。我想将所有图像显示为相同大小。

我使用了相对布局和嵌套表格布局,但由于某种原因,我无法将图像视图设置为相同大小。它总是以不同的尺寸显示它们。

...
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/button"
    android:layout_below="@+id/TextView">

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="???"
            android:layout_height="???"
            android:layout_weight="1"
        </ImageView>

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="???"
            android:layout_height="???"
            android:layout_weight="1">
        </ImageView>
    </TableRow>

    <TableRow>

        android:id="@+id/tableRow2"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="???"
            android:layout_height="???"
            android:layout_weight="1" >
        </ImageView>

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="???"
            android:layout_height="???"
            android:layout_weight="1" >
        </ImageView>
    </TableRow>

谢谢!

【问题讨论】:

    标签: android-layout imageview tablelayout


    【解决方案1】:

    您可以使用带有权重的 LinearLayout 来代替 TableLayout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false"
        android:orientation="vertical"
        android:weightSum="2" >
    
        <TextView
            android:id="@+id/textViewAnim"
            android:text="@string/my_text"
        android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <!-- 2 images in a row -->
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:baselineAligned="false"
            android:orientation="horizontal"
            android:weightSum="2" >
    
            <ImageView
                android:id="@+id/image1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
            android:src="@drawable/my_image" />
    
            <ImageView
                android:id="@+id/image2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
            android:src="@drawable/my_image" />
        </LinearLayout>
    
        <!-- ............... -->
    
    
        <!-- 2 images in a row -->
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:baselineAligned="false"
            android:orientation="horizontal"
            android:weightSum="2" >
    
            <ImageView
                android:id="@+id/image3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
            android:src="@drawable/my_image" />
    
            <ImageView
                android:id="@+id/image4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
            android:src="@drawable/my_image" />
        </LinearLayout>
    
    </LinearLayout>
    

    【讨论】:

    • @rafael,有用吗?
    【解决方案2】:
    <LinearLayout
        android:layout_width="409dp"
        android:layout_height="396dp"
        android:baselineAligned="false"
        android:orientation="vertical"
        android:weightSum="2"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="30dp">
    
            <ImageView
            android:id="@+id/image1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/animals" />
    
            <ImageView
            android:id="@+id/image2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/aquatic" />
    
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="458dp"
        android:layout_height="396dp"
        android:layout_weight="1"
        android:baselineAligned="false"
        android:orientation="vertical"
        android:weightSum="2"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="30dp">
    
            <ImageView
            android:id="@+id/image3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/flowers" />
    
            <ImageView
            android:id="@+id/image4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/birds" />
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-27
      • 1970-01-01
      • 2013-08-23
      相关资源
      最近更新 更多