【发布时间】: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