Android 使线性布局均匀分布图标,而不拉伸图标
我有一个水平的线性布局。带有4个图像按钮。我想把这些均匀地分布在屏幕的宽度上 通常我会将图标的布局宽度设置为0dp,权重设置为1。然后我将线性布局的wight sum设置为4,如下所示 但是,当我这样做时,它会拉伸按钮 如何在不拉伸图标、不使用相对布局和设置边距的情况下均匀分布它们Android 使线性布局均匀分布图标,而不拉伸图标,android,xml,android-layout,android-linearlayout,Android,Xml,Android Layout,Android Linearlayout,我有一个水平的线性布局。带有4个图像按钮。我想把这些均匀地分布在屏幕的宽度上 通常我会将图标的布局宽度设置为0dp,权重设置为1。然后我将线性布局的wight sum设置为4,如下所示 但是,当我这样做时,它会拉伸按钮 如何在不拉伸图标、不使用相对布局和设置边距的情况下均匀分布它们 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_con
<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>
感谢您的帮助在图像按钮之间使用虚拟视图
<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"/>
在图像按钮之间使用虚拟视图
<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"/>
我建议您使用
ImageView
而不是ImageButton
,然后使用android:background
而不是android:src
。然后使用合适的android:scaleType
你可以实现你想要的。我建议你使用ImageView
而不是ImageButton
,然后使用android:src
而不是android:background
来代替android:background
。然后使用合适的android:scaleType
你可以实现你想要的。使用宽度的wrap_内容,而不是0dp。由于图像大小的原因,拉伸按钮,为不同的屏幕大小设置不同的图像。只要大声想一想:如果这些是图像按钮,那么,图像的大小是否符合您在扩散后的要求?您是否尝试过在photoshop等照片编辑软件中创建ui?我不知道这是否有用!您只需将android:background
替换为android:src
当然可以,但您不必像shadoWalker所说的那样使用ImageView
,因为ImageButton
扩展了ImageView
使用wrap\u内容作为宽度,而不是0dp,因为图像大小的原因而拉伸按钮,为不同的屏幕大小设置不同的图像。只需大声想一想:如果这些是图像按钮,那么图像的大小是否符合您在传播后想要的大小?您是否尝试过在photoshop等照片编辑软件中创建ui?我不知道这是否有用!您只需将android:background
替换为android:src
当然,欢迎您,但您不必像shadoWalker所说的那样使用ImageView
,因为ImageButton
扩展了ImageView
我欣赏答案,但这似乎是一个巨大的内存浪费我欣赏答案,但是,这似乎是一个巨大的内存浪费。非常感谢您无需切换到ImageView
,因为ImageButton
扩展了ImageView
我建议这样做,因为ImageButton会导致背景问题。非常感谢您无需切换到ImageView
,因为ImageButton
扩展了ImageView
由于ImageButton引起的背景问题,我建议这样做。