Cam*_*ter 8 android android-layout android-xml
我意识到这可能是一个微不足道的布局问题,但我似乎无法按照我想要的方式工作,而没有分层我感觉太多的容器布局.我想要做的是设置一个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>
Run Code Online (Sandbox Code Playgroud)
结果:

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

但为了实现这一点,我不得不在我的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>
Run Code Online (Sandbox Code Playgroud)
在我看来,这是一个丑陋且笨拙的布局,不仅因为我现在不得不使用除了我的顶级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>
Run Code Online (Sandbox Code Playgroud)
仍然不是我希望的理想解决方案.
| 归档时间: |
|
| 查看次数: |
4294 次 |
| 最近记录: |