简体   繁体   中英

LinearLayout with rounded corners and background color

CompileSdkVersion 26, if it's important. I want to create LinearLayout with rounded corners, and background color.

I created 2 LinearLayouts, one with corners, and one with color, but their shapes do not match my expectations.

Code from activity

<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/whitegrey"
            android:layout_marginTop="340dp"
            android:id="@+id/layout"
            android:layout_marginStart="10dp">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/custom_border"
            android:orientation="vertical"
            android:padding="6dp"
            android:textColor="@color/black">
    ...
    </LinearLayout>
    </LinearLayout>

custom_border.xml:

<?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <corners android:radius="20dp"/>
        <padding android:left="8dp" android:right="8dp" android:top="8dp" android:bottom="8dp"/>
        <stroke android:width="2dp" android:color="#444444" />
    </shape>

Actually, there is rounded border, and behind, there is rectangular layout with target color. I want to fill with target color only what is inside borders. Is there any way to set the fill color, preferably in custom_border.xml?

Here, what I have at the moment:
在这里,我现在拥有的

Here, what I want to achieve:
在这里,我想要实现的目标

View from phone, the letters on the left are cut off:
从手机查看,左侧的字母被切断

From the code samples you provide us, it doesn't seem necessary to me to have two LinearLayout. Only the layout with android:background="@drawable/custom_border" is required.

To achieve the expected result, just add the property <solid android:color="@color/whitegrey" /> to your custom_border.xml :

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <corners android:radius="20dp"/>
    <padding android:left="8dp" android:right="8dp" android:top="8dp" android:bottom="8dp"/>
    <stroke android:width="2dp" android:color="#444444" />
    <solid android:color="@color/whitegrey" />
</shape>

I wish I was able to help you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM