我意识到这可能是一个琐碎的布局问题,但是如果不添加我认为太多的容器布局,似乎无法按照我想要的方式工作。我的目标是设置一个ImageView,满足以下条件:
- ImageView的宽度是其父级(现在是屏幕宽度)给定百分比的宽度。
- ImageView水平居中于屏幕。
我使用XML布局创建了一个测试用例,但实际上我将通过编程创建这些布局和视图;然而,为了找出正确的布局方法,我认为这并不重要。
我对Android布局中权重的使用有一定了解,以及它们通常用于具有相对权重因子的多个视图布局。为了满足条件1,我创建了一个容器LinearLayout(水平方向),设置其weightSum=1,然后嵌入我的ImageView,权重为我想要的任何百分比,例如0.5。 这可以正常工作!
接下来,我想让ImageView在水平方向上居中于屏幕。因此,我将重力设置为“center” /“ centerHorizontal”。这就是我卡住的地方,因为无论我选择哪种重力/ layout_gravity设置,它似乎总是靠左对齐。
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1.0">
<ImageView
android:src="@drawable/earth"
android:adjustViewBounds="true"
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_gravity="center_horizontal"
android:id="@+id/imageView1" />
</LinearLayout>
</LinearLayout>
结果:
我真正想要实现的是这样的效果:
但为了达到这个效果,我必须在我的ImageView两侧插入两个权重为0.25的虚拟LinearLayout,就像这样:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1.0">
<LinearLayout
android:orientation="horizontal"
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight="0.25"/>
<ImageView
android:src="@drawable/earth"
android:adjustViewBounds="true"
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_gravity="center_horizontal"
android:id="@+id/imageView1" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight="0.25"/>
</LinearLayout>
</LinearLayout>
在我看来,这是一个丑陋而笨拙的布局。不仅因为我现在需要使用3个额外的LinearLayout来调整我的视图大小和位置,而且因为我想要在程序中动态地完成所有的布局。我可能会决定在运行时将我的视图右对齐或左对齐,或者相对于屏幕宽度改变它们的缩放因子,在这种解决方案中,需要添加/删除虚拟布局视图并适当地设置所有权重。
我希望比我更擅长布局的人会有更好的解决方案!理想情况下,我只需要设置“gravity”(尽管我还没有能够使其生效),或者通过某种替代方式设置宽度,而无需水平LinearLayout容器和权重,因为没有该容器,我可以在垂直的顶级LinearLayout中水平居中。
谢谢!
编辑:我刚刚意识到,在我的第二个布局尝试中,使用虚拟填充LinearLayout,我可以省略第二个填充LinearLayout(设置为0.25的权重),只使用一个虚拟填充LinearLayout放在我的ImageView之前,因为它们是相对于父元素中预定的weightSum。例如:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1.0">
<LinearLayout
android:orientation="horizontal"
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight="0.25"/>
<ImageView
android:src="@drawable/earth"
android:adjustViewBounds="true"
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_gravity="center_horizontal"
android:id="@+id/imageView1" />
</LinearLayout>
这仍然不是我所期望的理想解决方案。
center_horizontal
? - gunar