简体   繁体   English

LinearLayout带有圆角和背景颜色

[英]LinearLayout with rounded corners and background color

CompileSdkVersion 26, if it's important. CompileSdkVersion 26,如果它很重要。 I want to create LinearLayout with rounded corners, and background color. 我想创建带有圆角和背景颜色的LinearLayout。

I created 2 LinearLayouts, one with corners, and one with color, but their shapes do not match my expectations. 我创建了2个LinearLayouts,一个有角,一个有颜色,但它们的形状与我的期望不符。

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: 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? 有没有办法设置填充颜色,最好是在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. 从您提供给我们的代码示例中,我似乎没有必要使用两个LinearLayout。 Only the layout with android:background="@drawable/custom_border" is required. 只需要使用android:background="@drawable/custom_border"的布局。

To achieve the expected result, just add the property <solid android:color="@color/whitegrey" /> to your custom_border.xml : 要获得预期的结果,只需将属性<solid android:color="@color/whitegrey" />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! 我希望我能帮到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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