【问题标题】:Make linear layout spread icons evenly without stretching the icons使线性布局均匀分布图标而不拉伸图标
【发布时间】:2016-11-06 06:45:37
【问题描述】:

我有一个水平线性布局。带有 4 个图像按钮。我想将这些均匀分布在屏幕的宽度上。

通常我会将图标的布局宽度设置为 0dp,将权重设置为 1。然后我会将线性布局的总和设置为 4,如下所示。

但是,当我这样做时,它会拉伸按钮。

如何在不拉伸图标、不使用相对布局和设置边距的情况下均匀分布它们?

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:weightSum="4">

        <ImageButton
            android:id="@+id/mainAlarmButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_alarm_white_18dp"/>

        <ImageButton
            android:id="@+id/addAlarmButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/ic_add_white_18dp"/>

        <ImageButton
            android:id="@+id/shareButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/ic_share_white_18dp"/>

        <ImageButton
            android:id="@+id/settingsButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/ic_tune_white_18dp"/>

    </LinearLayout>

感谢您的帮助

【问题讨论】:

  • 使用 wrap_content 代替 0dp
  • 因图片尺寸而拉伸按钮,为不同的屏幕尺寸设置不同的图片。
  • 只是想一想:如果这些是图像按钮,那么图像的大小是否根据您想要的传播后正确?您是否尝试过在 Photoshop 等照片编辑软件中创建用户界面?我不知道这是否有帮助!
  • 您只需将android:background 替换为android:src
  • 当然,不客气,但你不必像 shadoWalker 所说的那样使用 ImageView,因为 ImageButton 扩展了 ImageView

标签: android xml android-layout android-linearlayout


【解决方案1】:

我建议您使用ImageView 代替ImageButton,然后使用android:src 代替android:background。然后使用正确的android:scaleType,您可以实现您想要的。

【讨论】:

  • 非常感谢
  • 无需切换到ImageView,因为ImageButton 扩展了ImageView
  • 我建议因为 ImageButton 导致的背景问题。
【解决方案2】:

在 ImageButtons 之间使用虚拟视图

<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="5"
xmlns:android="http://schemas.android.com/apk/res/android">

<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="1"/>

<ImageButton
    android:id="@+id/mainAlarmButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_alarm_white_18dp"/>

<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="1"/>

<ImageButton
    android:id="@+id/addAlarmButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_add_white_18dp"/>
<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="1"/>

<ImageButton
    android:id="@+id/shareButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_share_white_18dp"/>
<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="1"/>

<ImageButton
    android:id="@+id/settingsButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_tune_white_18dp"/>
<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="1"/>

【讨论】:

  • 我很欣赏这个答案,但是,这似乎是一个很大的内存浪费
猜你喜欢
  • 1970-01-01
  • 2014-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多