我如何处理Android问题9161,其中bottomRightRadius和bottomLeftRadius被交换?

Jer*_*man 18 android rounded-corners

我的目标:

图1:目标
图1:目标

所以,在我知道这个问题之前,这就是我尝试过的.

一,基础布局:

<LinearLayout
    android:orientation="horizontal"
    android:layout_below="@id/heading"
    android:layout_marginTop="10dp"
    android:layout_width="@dimen/horizontal_two_button_width"
    android:layout_height="@dimen/button_height_small" >

    <Button
        android:id="@+id/button_one"
        android:layout_width="0dp"
        android:layout_weight="1.0"
        android:layout_height="fill_parent"
        android:padding="10dp"
        style="@style/ButtonText"
        android:background="@drawable/button_left_green" />

    <Button
        android:id="@+id/button_two"
        android:layout_width="0dp"
        android:layout_weight="1.0"
        android:layout_height="fill_parent"
        android:padding="10dp"
        style="@style/ButtonText"
        android:background="@drawable/button_right_green" />      

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

'button_left_green'drawable:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_left_green_pressed"
        android:state_pressed="true" />
    <item android:drawable="@drawable/button_left_green_focused"
        android:state_focused="true" />
    <item android:drawable="@drawable/button_left_green_default" />
</selector>
Run Code Online (Sandbox Code Playgroud)

并且,例如,'button_left_green_default'drawable:

<?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/shadow" />
            <corners
                android:radius="5dp"
                android:topLeftRadius="5dp"
                android:topRightRadius="0dp"
                android:bottomLeftRadius="5dp"
                android:bottomRightRadius="0dp" />      
        </shape>
    </item>
    <item
        android:bottom="19dp"
        android:top="1dp"
        android:left="1dp"
        android:right="1dp" >

        <shape android:shape="rectangle">
            <gradient
                android:startColor="@color/button_left_green_top_gradient_start"
                android:endColor="@color/button_left_green_top_gradient_end"
                android:angle="270" />

            <corners
                android:radius="5dp"
                android:topLeftRadius="5dp"
                android:topRightRadius="0dp"
                android:bottomLeftRadius="0dp"
                android:bottomRightRadius="0dp" />
        </shape>
    </item>

    <item
        android:top="19dp"
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp" >

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

            <corners
                android:radius="5dp"
                android:topLeftRadius="0dp"
                android:topRightRadius="0dp"
                android:bottomLeftRadius="5dp"
                android:bottomRightRadius="0dp" />
        </shape>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

所以,毕竟,我得到了图2中的图像:
图2:拿一个
图2:拿一个

在仔细检查了底角的定义之后,我确信在发现已知问题之前我一直很疯狂:http://code.google.com/p/android/issues/detail?id = 9161

我宁愿不只是交换它们,因为如果/当问题得到修复时,按钮将在新版本中被破坏.

我的一个想法是将实际按钮保留为常规矩形(即没有角半径)并用圆角矩形包裹两个按钮.我向LinearLayout添加了一个可绘制的背景,它具有圆角,但是按钮角重叠了LinearLayout圆边的边缘(参见图3).

图3:拿两个
图3:拿两个

如何将按钮的背景保持在其父级背景可绘制的范围内?或者你对如何解决这个bug有任何其他建议吗?

Jos*_*art 17

另一种解决方案是创建另一个名为"drawable-v12"的文件夹.

在这里放入正确的半径(so bottomLeftRadius,topLeftRadius),并在原始的drawable文件夹中放入交换的值.然后3.1+将使用v12文件夹,而3.1版本将使用drawable文件夹.


Jer*_*man 15

感觉就像这样的黑客,但它起作用了.

按钮最初由(1)外阴影,(2)上半部分渐变和(3)底部纯色组成.这是我做的:

  1. 制作顶部和底部两半,每个四角都是圆形的.这留下(1)左侧和右侧中间的间隙和(2)右侧的圆角.
  2. 创建一个小矩形以填充左中间的间隙.
  3. 创建另一个小矩形,以填充右中间的间隙,并在右侧正方形上制作顶角和底角.

这是左按钮正常状态的XML示例.

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

    <!-- Main part of button -->
    <item
        android:bottom="20dp"
        android:right="5dp" >

        <shape android:shape="rectangle">
            <solid android:color="@color/button_normal_green_top" />
            <corners android:radius="5dp" />
        </shape>
    </item>

    <item
        android:top="20dp"
        android:right="5dp" >

        <shape
            android:shape="rectangle" >
            <solid android:color="@color/button_normal_green_bottom" />
            <corners android:radius="5dp" />
        </shape>
    </item>

    <!-- Patch left middle part of button, which was left empty due to rounded 
    corners on top and bottom halves of button -->
    <item
        android:top="5dp"
        android:bottom="20dp"
        android:right="5dp" >

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

    <item
        android:top="20dp"
        android:bottom="5dp"
        android:right="5dp" >

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

    <!-- Patch right middle and make right side corners square -->
    <item
        android:bottom="20dp"
        android:left="15dp" >

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

    <item
        android:top="20dp"
        android:left="15dp" >

        <shape android:shape="rectangle" >
            <solid android:color="@color/button_normal_green_bottom" />
        </shape>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

然而,我确实失去了上半部分的渐变,但我可以使用双音按钮.结果如下:
两个按钮解决方案