Android 安卓:如何获得这种布局
有没有一种方法可以让图像像图中那样触动,而不用猜测每个水平线布局的高度 从我的XML中可以看出,我必须猜测144dp的线性布局高度才能获得这种效果,有没有更好的方法可以在不同的屏幕尺寸上工作Android 安卓:如何获得这种布局,android,android-layout,Android,Android Layout,有没有一种方法可以让图像像图中那样触动,而不用猜测每个水平线布局的高度 从我的XML中可以看出,我必须猜测144dp的线性布局高度才能获得这种效果,有没有更好的方法可以在不同的屏幕尺寸上工作 <LinearLayout android:id="@+id/LinearLayout" android:layout_width="match_parent" android:layout_height="wrap_content"
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="144dp" >
<ImageView
android:id="@+id/ImageView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_sport" />
<ImageView
android:id="@+id/ImageView02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_science" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout02"
android:layout_width="match_parent"
android:layout_height="144dp"
android:layout_gravity="fill_horizontal" >
<ImageView
android:id="@+id/imageView03"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_science" />
<ImageView
android:id="@+id/imageView04"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_sport" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout03"
android:layout_width="match_parent"
android:layout_height="144dp"
android:layout_gravity="fill_horizontal" >
<ImageView
android:id="@+id/ImageView05"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_sport" />
<ImageView
android:id="@+id/ImageView06"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_science" />
</LinearLayout>
</LinearLayout>
如果可能的话,我希望能够用XML来做。图像是正方形的,因此它们必须填充宽度,并且仍然保持比例。我几乎可以用fitXY来完成,但这只是延伸了它们的宽度,而不是高度。你应该使用网格视图,而不是许多线性布局 如果要保留代码,请更新以下内容: 主布局应具有以下属性:
android:layout_height="match_parent"
其他“布局包装器”应具有:
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content"
您的imageView应该具有:
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content"
您只处理方形图像吗?
我更新了你的代码,没有尝试,但应该可以:
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/ImageView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_sport" />
<ImageView
android:id="@+id/ImageView02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_science" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_science" />
<ImageView
android:id="@+id/imageView04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_sport" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:orientation="horizontal" >
<ImageView
android:id="@+id/ImageView05"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_sport" />
<ImageView
android:id="@+id/ImageView06"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_science" />
</LinearLayout>
</LinearLayout>
虽然图像不是完美的正方形(虽然它不明显),但我设法解决了这个问题,使用了2个垂直的
线性布局,而不是3个水平布局,并使用ScaleType=fitXY
来填补剩余的空白,这给我留下了:
从代码中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:weightSum="3" >
<ImageView
android:id="@+id/ImageView03"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="@drawable/ic_science" />
<ImageView
android:id="@+id/ImageView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="@drawable/ic_sport" />
<ImageView
android:id="@+id/ImageView02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="@drawable/ic_science" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:weightSum="3" >
<ImageView
android:id="@+id/ImageView04"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="@drawable/ic_sport" />
<ImageView
android:id="@+id/imageView03"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="@drawable/ic_science" />
<ImageView
android:id="@+id/imageView04"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="@drawable/ic_sport" />
</LinearLayout>
</LinearLayout>
您如何猜测线性布局的高度?你的意思是你在猜测线性布局的高度取决于其中图像的高度吗?你能将每个水平线性布局放在垂直线性布局中并实现你想要的吗?我增加了线性布局01的高度,直到图像完全填满线性布局(线性布局变成正方形)@cskoala使用垂直线性布局是一个很好的选择。它在垂直方向上填充屏幕,在两个垂直线性布局之间有微小的间隙。我可能可以通过使用scaleXYyeah来补偿这一点,您可以将边距更改为0。但不确定这是否能消除这些差距。你必须试一试。:)@cskoala图像是否完全呈正方形并不重要,因此我在所有图像上使用scaleType:fitXY来填补空白,正如您在我的回答中所看到的:)如果您使用的是layout\u weight
,则应根据您希望的设置layout\u height
或layout\u width
效果。我所有的图像都是方形的是的,我只需要它来处理方形的图像。你的代码不起作用:(它们回到垂直方向,但在水平方向上有很大的间隙