如何在整个图层列表周围创建边框?

时间:2015-07-03 01:47:12

标签: android android-layout

我试图在整个图层列表周围创建边框,而不是在图层列表中的各个形状上创建边框。我跟随这里的代码: http://gisinc.com/talk/creating-reusable-custom-toggle-button-android-applications/

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:right="@dimen/settings_toggle_circle_diameter">
        <shape android:shape="oval">
            <solid android:color="@color/black"/>
            <size
                android:height="@dimen/settings_toggle_circle_diameter"
                android:width="@dimen/settings_toggle_circle_diameter" />
        </shape>
    </item>
    <item android:left="@dimen/settings_toggle_circle_radius" android:right="@dimen/settings_toggle_circle_radius">
        <shape android:shape="rectangle">
            <solid android:color="@color/black"/>
            <size
                android:height="@dimen/settings_toggle_circle_diameter"
                android:width="@dimen/settings_toggle_circle_diameter" />
        </shape>
    </item>
    <item android:left="@dimen/settings_toggle_circle_diameter">
        <shape android:shape="oval">
            <solid android:color="@color/black"/>
            <size
                android:height="@dimen/settings_toggle_circle_diameter"
                android:width="@dimen/settings_toggle_circle_diameter" />
        </shape>
    </item>
</layer-list>

基本上我不希望在单个椭圆和矩形周围有边框,但整个边框周围有一个边框。有没有办法做到这一点?

2 个答案:

答案 0 :(得分:1)

三种可能的选择

  1. 使用stroke attribute of Shape和逻辑中的一些调整

  2. 用户内/外(边框)形状

  3. 使用九个补丁

  4. 我修改了您的代码以显示边框

    <?xml version="1.0" encoding="utf-8"?>
    <!-- I have assumed 1 dp padding around all the shapes-->
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- First Draw Left Outer circle(white) -->
        <item  android:right="22dp" android:left="0dp" android:top="0dp" android:bottom="0dp">
            <shape android:shape="oval">
                <solid android:color="@color/white"/>
                <size
                    android:height="22dp"
                    android:width="22dp" />
            </shape>
        </item>
        <!--  Draw Left Inner circle(black) -->
        <item  android:left="1dp" android:top="1dp" android:bottom="1dp" android:right="23dp">
            <shape android:shape="oval">
                <solid android:color="@color/black"/>
                <size
                    android:height="20dp"
                    android:width="20dp" />
            </shape>
        </item>
        <!-- Draw Right Outer circle(white) -->
        <item  android:left="22dp" android:right="0dp" android:top="0dp" android:bottom="0dp">
            <shape android:shape="oval">
                <solid android:color="@color/white"/>
                <size
                    android:height="22dp"
                    android:width="22dp" />
            </shape>
        </item>
        <!-- Draw Right Inner circle(black) -->
        <item android:left="23dp" android:right="1dp" android:top="1dp" android:bottom="1dp">
            <shape android:shape="oval">
                <solid android:color="@color/black"/>
                <size
                    android:height="20dp"
                    android:width="20dp" />
            </shape>
        </item>
        <!-- Draw Rectangle Center (black) -->
        <item android:left="11dp" android:right="11dp">
            <shape android:shape="rectangle">
                <solid android:color="@color/black"/>
                <size
                    android:height="20dp"
                    android:width="20dp" />
            </shape>
        </item>
        <!-- Draw Rectangle Top edge(white) -->
        <item android:left="11dp" android:right="11dp" android:bottom="21dp">
            <shape android:shape="rectangle">
                <solid android:color="@color/white"/>
            </shape>
        </item>
        <!-- Draw Rectangle Bottom edge(white) -->
        <item android:left="11dp" android:right="11dp" android:top="21dp">
            <shape android:shape="rectangle">
                <solid android:color="@color/white"/>
            </shape>
        </item>
    
    </layer-list>

答案 1 :(得分:1)

我想建议一种我觉得更容易的不同方法......

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="5dp"/>
            <padding android:left="15dp" android:right="15dp" android:top="15dp" android:bottom="15dp"/>
            <solid android:color="@color/primary"/>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="5dp"/>
            <solid android:color="@color/secondary"/>
        </shape>
    </item>
    <item android:gravity="center">
        <bitmap android:src="@drawable/logo_splash"
            android:gravity="center" />
    </item>
</layer-list>