带有 XML 的 Android 按钮样式背景

标签 android xml android-layout

所以我有两个按钮的样式非常接近我想要的样式,但我需要一点帮助才能完成。我现在有的是第一屏,我要的是第二屏。有人有主意吗?我附上了相关代码。

enter image description here enter image description here

布局 xml:

....
<Button
            android:id="@+id/sched_button"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/sync"
            android:layout_alignParentTop="true"
            android:background="@drawable/round_left_corner_active"
            android:text="@string/headersched"
            android:layout_toLeftOf="@+id/placeholder"
            android:paddingRight="@dimen/scheduleheadermargin"
            android:paddingLeft="@dimen/scheduleheadermargin" />

        <Button
            android:id="@+id/sched_fav_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/sched_button"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/placeholder"
            android:background="@drawable/round_right_corner"
            android:paddingLeft="@dimen/scheduleheadermargin"
            android:paddingRight="@dimen/scheduleheadermargin"
            android:text="@string/headerfav" />
...

round_left_corner_active:

<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/active" />

            <padding
                android:bottom="2dp"
                android:left="2dp"
                android:top="2dp" />

            <corners android:radius="5dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle" >
            <padding
                android:bottom="5dp"
                android:left="5dp"
                android:right="5dp"
                android:top="5dp" />

            <solid android:color="@color/active" />
        </shape>
    </item>
</layer-list>

round_right_corner

<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/active" />

            <padding
                android:bottom="2dp"
                android:right="2dp"
                android:top="2dp" />

            <corners android:radius="5dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle" >
            <padding
                android:bottom="5dp"
                android:left="5dp"
                android:right="5dp"
                android:top="5dp" />

            <solid android:color="@color/base" />
        </shape>
    </item>
</layer-list>

最佳答案

对于左按钮:

<corners
    android:radius="5dip"
    android:topRightRadius="0dip"
    android:bottomRightRadius="0dip" />  

正确的是:

<corners
    android:radius="5dip"
    android:topLeftRadius="0dip"
    android:bottomLeftRadius="0dip" />  

这应该可以解决问题。

但是,android:bottomRightRadiusandroid:bottomLeftRadius 在 API 低于 12 (Android 3.1 Honeycomb) 时有一个错误。您应该在 dimens.xml 中设置半径并创建一个新文件夹 values-v12。请参阅此解决方案:Something's wrong in Corner radius .
然后,你应该这样做:

向左绘制按钮

... 
<corners
    android:radius="5dip"
    android:topRightRadius="0dip"
    android:bottomRightRadius="@dimen/right_bottom_leftbutton"
    android:bottomLeftRadius="@dimen/left_bottom_leftbutton" /> 

右侧可绘制按钮

... 
<corners
    android:radius="5dip"
    android:topLeftRadius="0dip"
    android:bottomRightRadius="@dimen/right_bottom_rightbutton"
    android:bottomLeftRadius="@dimen/left_bottom_rightbutton" /> 

值/维度.xml

<!-- left button -->
<dimen name="right_bottom_leftbutton">5dip</dimen>
<dimen name="left_bottom_leftbutton">0dip</dimen>
<!-- right button -->
<dimen name="right_bottom_rightbutton">5dip</dimen>
<dimen name="left_bottom_rightbutton">0dip</dimen>

values-v12/dimens.xml

<!-- left button -->
<dimen name="right_bottom_leftbutton">0dip</dimen>
<dimen name="left_bottom_leftbutton">5dip</dimen>
<!-- right button -->
<dimen name="right_bottom_rightbutton">5dip</dimen>
<dimen name="left_bottom_rightbutton">0dip</dimen>

关于带有 XML 的 Android 按钮样式背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23120534/

相关文章:

android - 尝试为按钮设置 OnClickListener

java - OpenGL ES 2.0 中的立方体不绘制给定的纹理

使用架构 header 和目录查找的 Xml 验证

c# - 在可移植类库中包含 XPathSelectElement

xml - 如何使用XPath选择非空同级节点的笛卡尔乘积集?

android - ListView 使用 BaseAdapter

Android NDK 位置不可配置

java - 从 Google Map 获取结果并将数据传输回之前的 fragment

android - 如何将datePicker的值设置到EditText中?

android - WebView - 打断长词