gpt4 book ai didi

android - 在 Android 中使用 layout_weight 时图像对齐无法正常工作

转载 作者:搜寻专家 更新时间:2023-11-01 07:53:55 25 4
gpt4 key购买 nike

我将设计我的应用程序的仪表板,其中有几个菜单/按钮。菜单使用一些图像表示。菜单分为 4 列。首先,每列包含两个菜单,第四列包含三个。画面设计如下图:

dashboard image

现在为了开发这个,我尝试使用 Linearlayout 和 layout_weight。但是我无法放置它们并且图像被拉长了。我使用了以下代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/mumbaibg"
android:orientation="vertical"
android:weightSum="3" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal" >

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:background="@drawable/newcar" />

<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="@android:color/white" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:background="@drawable/usedcar" />
</LinearLayout>
</LinearLayout>

<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/white" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal" >

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:background="@drawable/carloan" />

<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="@android:color/white" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:background="@drawable/service" />
</LinearLayout>
</LinearLayout>

<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/white" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal" >

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:background="@drawable/sos" />

<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="@android:color/white" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:background="@drawable/learndrive" />
</LinearLayout>
</LinearLayout>

<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/white" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="horizontal" >

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:background="@drawable/insurance" />

<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="@android:color/white" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:background="@drawable/rtofinelist" />

<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="@android:color/white" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:background="@drawable/newsoffer" />
</LinearLayout>
</LinearLayout>

图像被放置在每一列上,但它们被拉伸(stretch)了。我尝试使用 .9patch 图像并将图像放在不同的可绘制文件夹中。

使用当前代码,屏幕看起来像这样:

screen with the current implementation

谁能给我一个更好、更简单的解决方案来实现这一目标。

谢谢,阿林达姆。

最佳答案

为了使缩放按预期工作,您需要为水平布局设置 android:layout_width="0dp" 并为水平布局设置 android:layout_height="0dp"垂直布局。

编辑,这里是实现设计的代码示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:orientation="horizontal">

<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:adjustViewBounds="true"
android:src="@drawable/ic_favorite" />

<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:adjustViewBounds="true"
android:src="@drawable/ic_favorite" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:orientation="horizontal">

<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:adjustViewBounds="true"
android:src="@drawable/ic_favorite" />

<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:adjustViewBounds="true"
android:src="@drawable/ic_favorite" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:orientation="horizontal">

<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:adjustViewBounds="true"
android:src="@drawable/ic_favorite" />

<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:adjustViewBounds="true"
android:src="@drawable/ic_favorite" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:orientation="horizontal">

<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:adjustViewBounds="true"
android:src="@drawable/ic_favorite" />

<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:adjustViewBounds="true"
android:src="@drawable/ic_favorite" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:adjustViewBounds="true"
android:src="@drawable/ic_favorite" />
</LinearLayout>

关于android - 在 Android 中使用 layout_weight 时图像对齐无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30852150/

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