Android 特定按钮布局
我想要这个具体的设计: 文本“A”、“B”和“C”居中 如果您提出xml解决方案,我将给出200点建议。它必须由3个按钮组成。我不需要java中的逻辑。这是我自己做的,但我需要xml绘图和布局 编辑Android 特定按钮布局,android,android-drawable,xml-drawable,Android,Android Drawable,Xml Drawable,我想要这个具体的设计: 文本“A”、“B”和“C”居中 如果您提出xml解决方案,我将给出200点建议。它必须由3个按钮组成。我不需要java中的逻辑。这是我自己做的,但我需要xml绘图和布局 编辑 请考虑向后兼容性和安卓5。您需要两种不同的形状,一种我们在左边的拐角处 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"
请考虑向后兼容性和安卓5。您需要两种不同的形状,一种我们在左边的拐角处
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/your_color"/>
<corners
android:topLeftRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
右边有一个圆角
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/your_color"/>
<corners
android:topRightRadius="0dp"
android:bottomRightRadius
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
作为按钮的背景。您可以在水平
线性布局上排列这三个按钮。要使三个布局具有相同的宽度,请放置布局\u width=“0dp”
和布局\u weight=“1”
您需要两种不同的形状,一种是我们在左边的拐角处
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/your_color"/>
<corners
android:topLeftRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
右边有一个圆角
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/your_color"/>
<corners
android:topRightRadius="0dp"
android:bottomRightRadius
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
作为按钮的背景。您可以在水平线性布局上排列这三个按钮。要使三个布局具有相同的宽度,请放置布局\u width=“0dp”
和布局\u weight=“1”
您需要创建三个xml绘图表
shape_left.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"/>
<stroke
android:color="@android:color/darker_gray"
android:width="1dp"/>
<solid
android:color="@android:color/transparent"/>
</shape>
shape_middle.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:color="@android:color/darker_gray"
android:width="1dp"/>
<solid
android:color="@android:color/transparent"/>
</shape>
shape_right.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:bottomRightRadius="5dp"
android:topRightRadius="5dp"/>
<stroke
android:color="@android:color/darker_gray"
android:width="1dp"/>
<solid
android:color="@android:color/transparent"/>
</shape>
在布局中:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/drawable_left"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/shape_middle"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/shape_right"/>
</LinearLayout>
不过要小心Android 5.0中的按钮,它可能会带来一些问题。但您可以将其作为任何视图的背景
我在安卓5.0上测试了它,效果很好。添加了透明颜色(可以是任何颜色)以支持旧版本。对于低于Android 4.0的版本,您需要创建一个drawable-v14文件夹,并将这些形状放在其中。在普通的可绘制文件夹中,您应该放置相同的形状,但是您应该执行bottomRightRadius,而不是bottomLeftRadius。同样的道理也适用于shape_right。这是因为有一个错误,它将底角转错了方向。您需要创建三个xml可绘制文件
shape_left.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"/>
<stroke
android:color="@android:color/darker_gray"
android:width="1dp"/>
<solid
android:color="@android:color/transparent"/>
</shape>
shape_middle.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:color="@android:color/darker_gray"
android:width="1dp"/>
<solid
android:color="@android:color/transparent"/>
</shape>
shape_right.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:bottomRightRadius="5dp"
android:topRightRadius="5dp"/>
<stroke
android:color="@android:color/darker_gray"
android:width="1dp"/>
<solid
android:color="@android:color/transparent"/>
</shape>
在布局中:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/drawable_left"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/shape_middle"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/shape_right"/>
</LinearLayout>
不过要小心Android 5.0中的按钮,它可能会带来一些问题。但您可以将其作为任何视图的背景
我在安卓5.0上测试了它,效果很好。添加了透明颜色(可以是任何颜色)以支持旧版本。对于低于Android 4.0的版本,您需要创建一个drawable-v14文件夹,并将这些形状放在其中。在普通的可绘制文件夹中,您应该放置相同的形状,但是您应该执行bottomRightRadius,而不是bottomLeftRadius。同样的道理也适用于shape_right。这是因为一个错误,它把底部的角落转错了方向。首先,我误解了你的问题。对我来说,似乎你需要一个布局,如你发布的图片所示。但是,在查看了android分段控件库之后,很明显您正在寻找一个允许在a
、B
、C
之间切换的控件。。。等等。库正在使用RadioGroup
,这确实是最好的选择
我注意到该库使用了负边距,我已经读到这会在某些设备上造成问题。还缺少对API 21的支持
xml方法的工作原理:RadioGroup
基于LinearLayout
。LinearLayout
的一个鲜为人知的特性(可能是因为它在大多数情况下不适用)是它的divider
属性。如果使用了android:showDividers=“…”
,则LinearLayout
将显示分隔符及其子项。显示这些分隔符的位置取决于给定给showDivider=“…”
的值。有三个值是可能的:中间
,开始
和结束
。我们将使用middle
来显示A
和B
与B
和C
之间的分隔符
对于有线框架,我们不需要几个可抽绳(至少现在不需要)。我们可以采取以下措施:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:color="@color/stroke_color" android:width="@dimen/stroke_width"/>
<corners android:radius="@dimen/corner_radius"/>
</shape>
现在,我们需要注意单选按钮的活动状态。考虑到圆角,我们需要编码3种不同的可绘制图形:一个用于第一个孩子,一个用于中间,一个用于最后一个。这种方法是可伸缩的——第一个和最后一个可绘制的保持不变,而第一个和最后一个之间的每个子级都可以获得中间的可绘制。我创建了几个GIST,您可以将其添加到项目中:
API<21-将其放置在res/drawable
中:
API>=21-将其放入res/drawable-v21
:
资源-包含定义的颜色、尺寸和样式-您可以将此文件放置在res/values
中,也可以将每个资源类型复制粘贴到其各自的文件中:
最后,这里是一个示例xml布局,展示了定义的样式如何实现这一点:
在API级别<21时起作用:
就API 21采取行动:
首先,我误解了你的问题。对我来说,似乎你需要一个布局,如你发布的图片所示。但是,在查看了android分段控件库之后,很明显您正在寻找一个允许在a
、B
、C
之间切换的控件。。。等等。库正在使用RadioGroup
,这确实是最好的选择
我注意到该库使用了负边距,我已经读到这会在某些设备上造成问题。还缺少对API 21的支持
xml方法的工作原理:RadioGroup
基于LinearLayout
。LinearLayout
的一个鲜为人知的特性(可能是因为它在大多数情况下不适用)是它的分隔符