Android 按钮间距
我试图将按钮的边距设置为0,因此按钮之间没有间距 基本上,我希望我的按钮具有以下样式和颜色: 你知道我怎样才能完成这种任务吗?我不想创建一个9补丁自己的形象,因为我没有任何知识这样做 你知道我怎样才能完成这种任务吗?我不想创建一个9补丁自己的形象,因为我没有任何知识这样做 恐怕你别无选择。每个按钮之间的固有间距是框架使用的现有9块背景中直接构建的超透明像素的结果。要替换此行为,必须将按钮的背景设置为不包含此固有间距的其他可绘制对象Android 按钮间距,android,layout,button,margin,Android,Layout,Button,Margin,我试图将按钮的边距设置为0,因此按钮之间没有间距 基本上,我希望我的按钮具有以下样式和颜色: 你知道我怎样才能完成这种任务吗?我不想创建一个9补丁自己的形象,因为我没有任何知识这样做 你知道我怎样才能完成这种任务吗?我不想创建一个9补丁自己的形象,因为我没有任何知识这样做 恐怕你别无选择。每个按钮之间的固有间距是框架使用的现有9块背景中直接构建的超透明像素的结果。要替换此行为,必须将按钮的背景设置为不包含此固有间距的其他可绘制对象 另一个可以在代码中完成的选项是创建用于每个背景的XML可绘制形状
另一个可以在代码中完成的选项是创建用于每个背景的XML可绘制形状。您可以创建一个具有角半径、填充渐变和笔划的单独形状,就像您的图像一样。您可以阅读有关创建的更多信息。在本例中,您可以使用XMLs轻松完成此任务。 这是如何通过两个步骤实现的: 第一步 在可绘制文件夹中创建3个形状: 第一个形状用于左按钮:shape_button_left.xml。此形状具有径向左角和渐变背景
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#BFBFBF" >
</stroke>
<corners
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp" >
</corners>
<gradient android:startColor="#D2D2D2" android:endColor="#F2F2F2" android:angle="90"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#BFBFBF" >
</stroke>
<gradient android:startColor="#D2D2D2" android:endColor="#F2F2F2" android:angle="90"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#BFBFBF" >
</stroke>
<corners
android:bottomRightRadius="10dp"
android:topRightRadius="10dp" >
</corners>
<gradient android:startColor="#D2D2D2" android:endColor="#F2F2F2" android:angle="90"/>
</shape>
第二个形状用于中心按钮:shape_button_center.xml。此形状不为角定义任何内容,并且具有渐变背景
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#BFBFBF" >
</stroke>
<corners
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp" >
</corners>
<gradient android:startColor="#D2D2D2" android:endColor="#F2F2F2" android:angle="90"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#BFBFBF" >
</stroke>
<gradient android:startColor="#D2D2D2" android:endColor="#F2F2F2" android:angle="90"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#BFBFBF" >
</stroke>
<corners
android:bottomRightRadius="10dp"
android:topRightRadius="10dp" >
</corners>
<gradient android:startColor="#D2D2D2" android:endColor="#F2F2F2" android:angle="90"/>
</shape>
第三个形状用于右按钮:shape_button_right.xml。此形状具有径向右角和渐变背景
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#BFBFBF" >
</stroke>
<corners
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp" >
</corners>
<gradient android:startColor="#D2D2D2" android:endColor="#F2F2F2" android:angle="90"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#BFBFBF" >
</stroke>
<gradient android:startColor="#D2D2D2" android:endColor="#F2F2F2" android:angle="90"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#BFBFBF" >
</stroke>
<corners
android:bottomRightRadius="10dp"
android:topRightRadius="10dp" >
</corners>
<gradient android:startColor="#D2D2D2" android:endColor="#F2F2F2" android:angle="90"/>
</shape>
步骤2
现在,我们可以在简单视图中使用这些形状来获得按钮的效果。
在布局XML中添加下一个代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<!-- Button Left -->
<LinearLayout
android:id="@+id/button_left"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@drawable/shape_button_left"
android:gravity="center"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left"
android:textColor="#333333"
android:textSize="20sp" />
</LinearLayout>
<!-- End Button Left -->
<!-- Button Center -->
<LinearLayout
android:id="@+id/button_center"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@drawable/shape_button_center"
android:gravity="center"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Center"
android:textColor="#333333"
android:textSize="20sp" />
</LinearLayout>
<!-- End Button Center -->
<!-- Button Right -->
<LinearLayout
android:id="@+id/button_right"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@drawable/shape_button_right"
android:gravity="center"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right"
android:textColor="#333333"
android:textSize="20sp" />
</LinearLayout>
<!-- End Button Right -->
</LinearLayout>
就这样
现在,您可以在代码中将onClick listener添加到LinearLayouts中,并像按钮一样使用它
在我的手机上测试此代码会得到下一个结果: