8

我意识到这可能是一个微不足道的布局问题,但如果不分层我觉得太多的容器布局,我似乎无法让它按照我想要的方式工作。我想要做的是设置一个 ImageView 以满足以下条件:

  1. ImageView 的宽度是其父级宽度的某个给定百分比(目前为屏幕宽度)。
  2. ImageView 在屏幕内水平居中。

我已经使用 XML 布局创建了一个测试用例,但实际上我将以编程方式创建这些布局和视图;不过,我认为这对于找出正确的布局并不重要。

我对 Android 布局中的权重以及它们通常如何用于布局具有相对权重因子的多个视图有些熟悉。为了满足条件1,我创建了一个容器LinearLayout(水平方向),设置它的weightSum = 1,然后用我想要的任何百分比的权重嵌入我的ImageView,比如0.5。这行得通!

接下来,我希望 ImageView 最终在屏幕中水平居中。所以我将重力设置为“center”/“centerHorizo​​ntal”。这就是我卡住的地方,因为无论我选择什么重力/布局重力设置,它似乎总是与左侧对齐。

布局文件:

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

在我看来,这是一个丑陋而尴尬的布局,不仅因为我现在必须使用除顶级 LinearLayout 之外的 3 个额外的 LinearLayout 来调整视图的大小和位置,还因为我想以编程方式完成所有布局并且是动态的。我可能决定在运行时右对齐或左对齐我的视图,或者更改它们相对于屏幕宽度的缩放因子,在此解决方案中,这可能需要添加/删除虚拟布局视图并适当地设置所有权重。

我希望比我更擅长布局的人会有更好的解决方案!理想情况下,我可以设置“重力”(尽管我无法让它工作),或者一些替代方法来设置宽度而不需要水平 LinearLayout 容器和权重,因为没有那个容器我可以居中在我的顶级垂直 LinearLayout 中水平放置。

谢谢!

编辑:我刚刚意识到,在我的第二次布局尝试使用虚拟填充 LinearLayouts 时,我可以取消第二个填充 LinearLayout(设置为 0.25 的权重),并且只在 ImageView 之前使用一个虚拟填充 LinearLayout,因为它们是相对于预定的父级中的 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>

仍然不是我希望的理想解决方案。

4

2 回答 2

8

你可以添加android:gravity="center"到你的线性布局(不在 imageView 中)

我希望它有所帮助。

于 2013-08-29T15:17:38.607 回答
0

ImageView 的重力不居中,因为父 LinearLayout 是水平的。

您想要设置父 LinearLayout 的重力,该重力将在其父级(垂直 LinearLayout)内水平居中。

于 2013-08-29T15:21:15.540 回答