【问题标题】:Specific Button Layout特定按钮布局
【发布时间】:2015-03-17 13:29:00
【问题描述】:

我想要这个特定的设计:

文本“A”、“B”和“C”居中。

如果您以 xml 格式提出解决方案,我提供 200 分。它必须由 3 个按钮组成。我不需要java中的逻辑。这我可以自己制作,但我需要 xml 可绘制对象和布局。

编辑

请考虑向后兼容性和 android 5。

【问题讨论】:

标签: android android-drawable xml-drawable


【解决方案1】:

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

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

xml 方法的工作原理:RadioGroup 是基于 LinearLayout 构建的。 LinearLayout 的一个鲜为人知的特性(可能是因为它大部分时间不适合)是它的 divider 属性。如果使用android:showDividers="..."LinearLayout 将显示分隔线及其子项。这些分隔符的显示位置取决于showDivider=".." 的值。可能有三个值:middlebeginningend。我们将使用middle 来显示ABBC 之间的分隔线。

对于有线框架,我们不需要多个可绘制对象(至少,现在不需要)。我们可以这样做:

<?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,我们还需要提供一个dividerdrawable:

<?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 个不同的可绘制对象:一个用于第一个孩子,一个用于中间,一个用于最后一个。这种方法是可扩展的——第一个和最后一个可绘制对象保持不变,而第一个和最后一个之间的每个孩子都获得中间可绘制对象。我创建了几个可以添加到项目中的要点:

API 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

资源 - 包含已定义的颜色、尺寸和样式 - 您可以将此文件放在 res/values 中或将每种资源类型复制粘贴到其各自的文件中:

Resources

最后,这是一个示例 xml 布局,它显示了定义的样式如何实现这一点:

Sample

在 API 级别

pre_lollipop.mp4

在 API 21 上的行动:

lollipop.mp4

【讨论】:

  • 哦,嘘。现在我只能开始 200 的赏金。知道我怎么能给 100 吗?回答我的任何问题中重复的内容,我会在那里给你。
  • @zatziky :)) 嘿,你不必那样做。我回答是因为我认为这是一个有趣的问题。不要担心积分。顺便问一下,我的回答有用吗?
  • 是的,它很有用。 ;) 接受!
【解决方案2】:

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

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

【讨论】:

  • 酷,谢谢。这个周末有时间我会试试的。如果我必须将您的解决方案与其他人的解决方案结合起来,我会分分。
【解决方案3】:

你需要创建三个 xml drawables

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 也是如此。这是因为一个错误导致底角转向错误。

【讨论】:

  • 酷,谢谢。这个周末有时间我会试试的。如果我必须将您的解决方案与其他人的解决方案结合起来,我会分分。
  • @zatziky 我在 Android 5.0 和一些更早的手机上测试过它。我更改了答案以支持向后兼容性
  • 如何在每个shape_*.xml中干预活动时的背景颜色变化?
  • 如果您想要活动阶段,您可以更好地将其更改为单选按钮实现:github.com/hoang8f/android-segmented-control 如果您不希望此库搜索 uisegmentedcontrol android
  • 我将奖励你赏金。但明天有可能。别担心! ;)
猜你喜欢
  • 2014-01-27
  • 1970-01-01
  • 1970-01-01
  • 2014-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-26
  • 1970-01-01
相关资源
最近更新 更多