具体按钮布局

Specific Button Layout

我想要这样的设计:

文本 "A"、"B" 和 "C" 居中。

如果你能在xml中提出解决方案,我给200分。它必须由 3 个按钮组成。我不需要 java 中的逻辑。这个我可以自己做,但我需要 xml 绘图和布局。

编辑

请考虑向后兼容性和android5。

你需要两种不同的形状,一种是左边的圆角

<?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>

作为按钮的背景。您可以将三个按钮排列在水平 LinearLayout 上。要给三个相同的宽度,请放置 layout_width="0dp"layout_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 中的按钮,它可能会产生一些问题。但是你可以把它作为任何视图的背景。

我在 Android 5.0 上测试过它,它可以工作。添加透明颜色(可以是任何颜色)以支持旧版本。对于 Android 4.0 以下的版本,您需要创建一个文件夹 drawable-v14 并将这些形状放在那里。在普通的可绘制文件夹中,您应该放置相同的形状,但您应该使用 bottomRightRadius 而不是 bottomLeftRadius。 shape_right 也是如此。这是因为一个将底角转错方向的错误。

起初,我误读了你的问题。对我来说,您似乎需要一个如您发布的图片所示的布局。但是,在查看 android-segmented-control 库之后,很明显您正在寻找一个允许在 ABC... 等之间切换的控件。图书馆正在使用 RadioGroup 这确实是最好的选择。

我注意到该库使用负边距,据我所知这会导致某些设备出现问题。也缺少对 API 21 的支持。

xml 方法如何工作:RadioGroup 建立在 LinearLayout 之上。 LinearLayout 的一个鲜为人知的特性(可能是因为它在大多数时候不适用)是它的 divider 属性。如果使用 android:showDividers="..."LinearLayout 将显示分隔符及其 children。这些分隔符的显示位置取决于 showDivider=".." 的值。可能有三个值:middlebeginningend。我们将使用 middle 来显示 AB 以及 BC 之间的分隔线。

对于连线框架,我们不需要多个可绘制对象(至少现在不需要)。我们可以做以下事情:

<?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>

上面的drawable将被设置为RadioGroup的背景showDividers 将负责 ABC 之间的垂直划分。因此,我们的布局看起来与您现在发布的图片相同。那么,有了 showDividers,我们还需要提供一个 divider 可绘制对象:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@color/stroke_color"/>
    <size android:width="@dimen/stroke_width"/>
</shape>

现在,我们需要处理 RadioButtons 的活动状态。鉴于圆角,我们需要编写 3 个不同的 drawable:一个用于第一个 child,一个用于中间,一个用于最后一个。这种方法是可扩展的——第一个和最后一个可绘制对象保持不变,而第一个和最后一个之间的每个 child 获取中间可绘制对象。我已经创建了几个要点,您可以将它们添加到您的项目中:

API < 21 - 将这些放在 res/drawable:

RadioGroup background

RadioGroup divider

RadioButton background for first child

RadioButton background for middle child

RadioButton background for last child

API >= 21 - 将这些放在 res/drawable-v21:

RadioButton background for first child

RadioButton background for middle child

RadioButton background for last child

Resources - 包含定义的颜色、尺寸和样式 - 您可以将此文件放在 res/values 或 copy-paste 每种资源类型中文件:

Resources

最后,这里有一个 xml 布局示例,展示了定义的样式如何实现这一点:

Sample

正在 API 级别 < 21:

pre_lollipop.mp4

在 API 21 日行动:

lollipop.mp4