Android ImageView按重量调整大小,不以父LinearLayout为中心

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

> ImageView的宽度是其父级宽度的一定百分比(现在的屏幕宽度).
> 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的虚拟LinearLayouts,如下所示:

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

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

解决方法

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

我希望它有所帮助.

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...