gpt4 book ai didi

Android ImageView 按重量大小不在父 LinearLayout 居中

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:06:54 24 4
gpt4 key购买 nike

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

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

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

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

接下来,我希望 ImageView 最终在屏幕上水平居中。所以我将重力设置为“center”/“centerHorizo​​ntal”。这就是我卡住的地方,因为无论我选择什么 gravity/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>

结果:

Resulting image aligned to the left

我真正想要实现的是这样的:

Desired result

但为了实现这一点,我不得不在我的 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 个额外的 LinearLayouts 除了我的顶级 LinearLayout 来调整我的 View 的大小和位置,而且因为我想做所有的事情以编程方式和动态布局。我可能会决定在运行时右对齐或左对齐我的 View ,或者更改它们相对于屏幕宽度的比例因子,在此解决方案中,这可能需要添加/删除虚拟布局 View 并适本地设置所有权重。

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

谢谢!

编辑: 我刚刚意识到,在我第二次使用虚拟填充 LinearLayouts 的布局尝试中,我可以取消第二个填充 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>

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

最佳答案

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

希望对你有帮助。

关于Android ImageView 按重量大小不在父 LinearLayout 居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18514697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com