Android 具有相等填充的线性布局中的子级
我目前正在设计一个有5个按钮的按钮栏(它们都是ImageButtons,但目前只有3个)。这是我的第一个安卓项目,所以我边学习边学习。我试图在不缩放按钮的情况下对每个按钮平均加权(具有相同的填充而不是相同的宽度)。这是我目前的代码:Android 具有相等填充的线性布局中的子级,android,xml,layout,Android,Xml,Layout,我目前正在设计一个有5个按钮的按钮栏(它们都是ImageButtons,但目前只有3个)。这是我的第一个安卓项目,所以我边学习边学习。我试图在不缩放按钮的情况下对每个按钮平均加权(具有相同的填充而不是相同的宽度)。这是我目前的代码: <LinearLayout android:id="@+id/back" android:orientation="horizontal" android:layout_height="50dip" android:layout_width="fill
<LinearLayout
android:id="@+id/back"
android:orientation="horizontal"
android:layout_height="50dip"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:background="#000000"
style="@android:style/ButtonBar"
android:weightSum="5"
>
<ImageButton
android:id="@+id/info_button"
android:padding="20dp"
android:background="@drawable/info"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
/>
<Button
android:id="@+id/wishlist_button"
android:text="@string/wish_label"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:padding="20dp"
android:textSize="15dip"
android:textColor="#b7b7b7"></Button>
<Button
android:id="@+id/buy_button"
android:text="@string/buy_label"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:padding="20dp"
android:textSize="15dip"
android:textColor="#b7b7b7"/>
<ImageButton
android:id="@+id/dislike_button"
android:padding="20dp"
android:background="@drawable/dislike_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
/>
<ImageButton
android:id="@+id/like_button"
android:padding="20dp"
android:background="@drawable/like_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
/>
这就是它现在的样子:
拉伸加权:
这就是我希望它看起来的样子(非常接近):
非拉伸均匀填充:
谢谢你的帮助。我寻找答案已经有一段时间了,已经尝试了很多 您可以在每个图像/按钮之间添加间隔元素,并将布局权重分配给间隔元素,而不是图像/按钮。所以它看起来像这样:
<LinearLayout
android:id="@+id/back"
android:orientation="horizontal"
android:layout_height="50dip"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:background="#000000"
style="@android:style/ButtonBar"
>
<ImageButton
android:id="@+id/info_button"
android:padding="20dp"
android:background="@drawable/info"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_weight="1" />
/>
<Button
android:id="@+id/wishlist_button"
android:text="@string/wish_label"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="20dp"
android:textSize="15dip"
android:textColor="#b7b7b7"></Button>
<LinearLayout
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/buy_button"
android:text="@string/buy_label"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="20dp"
android:textSize="15dip"
android:textColor="#b7b7b7"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageButton
android:id="@+id/dislike_button"
android:padding="20dp"
android:background="@drawable/dislike_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageButton
android:id="@+id/like_button"
android:padding="20dp"
android:background="@drawable/like_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
</LinearLayout>
/>
这可能不是最优雅的解决方案,因为它在你的布局中增加了几个视图,但是它对我来说是过去的工作。
你应该考虑只使用相同大小的图像按钮,在线性布局内使用等权重的图像。
问候,, 斯泰芬尼在这里:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/back"
android:orientation="horizontal"
android:layout_height="50dip"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:background="#000000"
style="@android:style/ButtonBar"
android:gravity="center"
>
<ImageButton
android:id="@+id/info_button"
android:background="@null"
android:src="@drawable/info"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
/>
<Button
android:id="@+id/wishlist_button"
android:text="@string/wish_label"
android:background="@null"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:textSize="15dip"
android:textColor="#b7b7b7"></Button>
<Button
android:id="@+id/buy_button"
android:text="@string/buy_label"
android:background="@null"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:textSize="15dip"
android:textColor="#b7b7b7"/>
<ImageButton
android:id="@+id/dislike_button"
android:background="@null"
android:src="@drawable/dislike_button"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
/>
<ImageButton
android:id="@+id/like_button"
android:background="@null"
android:src="@drawable/like_button"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
/>
</LinearLayout>
我不知道style=“@android:style/ButtonBar”是如何设置的,所以如果它没有正确显示,请尝试删除它