如何防止LinearLayout Android菜单中按钮之间出现空格?
我在RelativeLayout中有LinearLayout,因此我想将按钮放置在父布局的顶部。 我添加了4个相对权重相等的按钮(“1”),因此它们的大小都相同。 现在它显示为这样,但我想要一个没有空格的外观如何防止LinearLayout Android菜单中按钮之间出现空格?,android,button,spaces,Android,Button,Spaces,我在RelativeLayout中有LinearLayout,因此我想将按钮放置在父布局的顶部。 我添加了4个相对权重相等的按钮(“1”),因此它们的大小都相同。 现在它显示为这样,但我想要一个没有空格的外观 这些按钮没有任何空格。这是因为背景可绘制包含在左侧和右侧的透明填充。 将按钮的背景更改为某种颜色(例如android:background=“#f01123”),您将看到按钮之间没有空格。如果您未能设置按钮的背景,或者将透明或某种颜色设置为背景,则会出现此问题。请不要让它为空,如果你这样做
这些按钮没有任何空格。这是因为背景可绘制包含在左侧和右侧的透明填充。
将按钮的背景更改为某种颜色(例如android:background=“#f01123”),您将看到按钮之间没有空格。如果您未能设置按钮的背景,或者将透明或某种颜色设置为背景,则会出现此问题。请不要让它为空,如果你这样做,默认按钮属性将被分配。我已经为你们准备好了样品。请试试这个
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/white"
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/white"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@android:color/white"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@android:color/white"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
在线性布局(android:weightsum=“4”)上设置权重和,并在每个按钮上设置宽度匹配父项。这不是答案,但我认为你应该这样做:线性布局应该有方向(垂直、水平),因为线性布局用于组织你的应用程序,这是一种简单的方法。如果您的方向是垂直的,则按钮将位于彼此的顶部。只需更改按钮样式/背景即可实现此目的。此操作不起作用。如果我改变颜色并设置android:Background=color,那么整个按钮都会改变..亲爱的Chandru,谢谢,尽管逻辑有点奇怪和不方便(因为我不明白为什么按钮的默认背景是不可见的..左右),但是现在,按钮是不可点击的。。或者它们是,但在onClick过程中不会改变颜色。为此,您必须放置选择器背景。请参阅“使用选择器背景”按钮您在开发人员网站上有完整的解决方案。请参考。如果你有任何疑问,请告诉我。