安卓:四个正方形的对齐方式

9
我是一名有用的助手,可以为您进行文本翻译。以下是需要翻译的内容:

我正在尝试在Android屏幕上对齐四个大小相等的正方形,我已经尝试了感觉像是数百万种不同的方法,但似乎没有一个方法能够奏效 :(。

目前我所拥有的是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:id="@+id/MasterLayout" android:layout_width="wrap_content"     android:layout_height="fill_parent"  android:background="#FFFFFF">
<TableLayout android:layout_width="fill_parent" android:id="@+id/mainlay" android:background="#444444" android:layout_weight="0.2" android:layout_height="wrap_content" android:padding="0dip">
    <TableRow android:layout_weight="1"  android:background="#BBBBBB" android:padding="0dip">
        <ImageView android:id="@+id/ImageView1" android:layout_marginLeft="10px" android:layout_weight="1" android:layout_marginRight="5px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:scaleType="centerInside" android:src="@drawable/bigbox_new"></ImageView>
        <ImageView android:id="@+id/ImageView2" android:layout_marginLeft="5px" android:layout_weight="1" android:layout_marginRight="10px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:scaleType="centerInside" android:src="@drawable/bigbox_new"></ImageView>
    </TableRow>
    <TableRow android:layout_weight="1" android:padding="0dip">
        <ImageView android:id="@+id/ImageView3" android:layout_marginLeft="10px" android:layout_weight="1" android:layout_marginRight="5px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:scaleType="centerInside" android:src="@drawable/bigbox_new"></ImageView>
        <ImageView android:id="@+id/ImageView4" android:layout_marginLeft="5px" android:layout_weight="1" android:layout_marginRight="10px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:scaleType="centerInside" android:src="@drawable/bigbox_new"></ImageView>
    </TableRow>
</TableLayout>
</LinearLayout>

这基本上已经完成了任务。但是,这四张图片每一张都有很大的上下填充。我该如何摆脱它?或者我需要使用不同的布局类型吗?
为了帮助说明我的问题,这里有一张图片。 左边是我得到的,右边是我需要的。 Image 非常感谢!
祝好,马库斯!
3个回答

3

请从您的TableRows中删除layout_weight,并将它们的layout_height设置为wrap_content。然后在它们下面添加一个空的TableRow,将layout_height设置为fill_parent


谢谢!使用fill_parent布局的技巧正是我所需要的。 - metter

0

如果图像大小大于所需尺寸,可以尝试使用scaleType作为centerCrop,这将对您有所帮助。但这不是最好的方法,更好的方法是更改图像资源。

希望对您有所帮助!


你好,Karan。感谢你的提示。然而,我的描述似乎不太清晰(尤其是图片);我需要正方形保持正方形。它们应该尽可能大地放在2x2的网格中。但屏幕上剩余的空间不能在四个正方形之间,而是在屏幕底部的它们下面。你能给我指点吗?谢谢。 - metter

0

尝试将ImageView的高度设置为fill_parent,我认为这应该可以解决问题。无论如何,如果您的XML属性被分成几行,对于我们其他人来说阅读单行属性很困难。

另外,为什么要在LinearLayout中放置TableLayout?除非您在该Linear中有更多元素,否则可以将其删除。


很遗憾,这没有产生期望的效果。无论如何,感谢您的时间! @ Code; 你是对的。我现在已经重构成了几行代码。 - metter

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