应用自定义背景到具有圆角的布局时出现问题。

huangapple go评论67阅读模式
英文:

problem in applying custom background to layout with radius in corners

问题

我想创建一个类似这样的设计:

交付消息是一个带有自定义背景的TextView,它运行正常,但在底部有一个ConstraintLayout,当我使用为其制作的自定义背景时,它看起来不正常,角落里有阴影。这个自定义背景在按钮上应用正常,但当我将它用于ConstraintLayout时,它的效果不如预期。

这里是自定义的可绘制XML:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/turquoise" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <corners
                android:bottomLeftRadius="0dp"
                android:bottomRightRadius="0dp"
                android:topLeftRadius="14dp"
                android:topRightRadius="14dp" />
            <padding
                android:left="5dp"
                android:top="5dp"
                android:right="5dp"
                android:bottom="5dp"    >
            </padding>
            <solid android:color="@color/white"
                android:elevation="0dp"
                />
            <size android:height="60dp" />
        </shape>
    </item>
</layer-list>

你如何将它应用到ConstraintLayout?

英文:

I want to make a design like that 应用自定义背景到具有圆角的布局时出现问题。

the delivery msg is a texview with custom background and it works fine but in the bottom of it there is a constraint-layout and when I used the custom background I made for it it didn't look fine there is a shadow in the corner, this custom background was applied fine before to a button but when I use it to constraint-layout it didn't works as expect

应用自定义背景到具有圆角的布局时出现问题。

here is the custom drawable xml

&lt;layer-list xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
&lt;item&gt;
    &lt;shape android:shape=&quot;rectangle&quot;&gt;
        &lt;solid android:color=&quot;@color/turquoise&quot; /&gt;
    &lt;/shape&gt;
&lt;/item&gt;
&lt;item&gt;
    &lt;shape android:shape=&quot;rectangle&quot;&gt;
        &lt;corners
            android:bottomLeftRadius=&quot;0dp&quot;
            android:bottomRightRadius=&quot;0dp&quot;
            android:topLeftRadius=&quot;14dp&quot;
            android:topRightRadius=&quot;14dp&quot; /&gt;
        &lt;padding
            android:left=&quot;5dp&quot;
            android:top=&quot;5dp&quot;
            android:right=&quot;5dp&quot;
            android:bottom=&quot;5dp&quot;    &gt;
        &lt;/padding&gt;
        &lt;solid android:color=&quot;@color/white&quot;
            android:elevation=&quot;0dp&quot;
            /&gt;
        &lt;size android:height=&quot;60dp&quot; /&gt;
    &lt;/shape&gt;
&lt;/item&gt;

</layer-list>

how can I apply that to constraint-layout

答案1

得分: 0

解决方案 1:
对颜色为turquoise的矩形应用圆角和填充。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/turquoise" />
            <padding android:top="28dp" />
            <corners
                android:topLeftRadius="14dp"
                android:topRightRadius="14dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <corners
                android:bottomLeftRadius="0dp"
                android:bottomRightRadius="0dp"
                android:topLeftRadius="14dp"
                android:topRightRadius="14dp" />
            <padding
                android:bottom="5dp"
                android:left="5dp"
                android:right="5dp"
                android:top="5dp" />
            <solid
                android:color="@color/white"
                android:elevation="0dp" />
            <size android:height="60dp" />
        </shape>
    </item>
</layer-list>

请注意,android:top的值需要根据您的需求进行设置。

解决方案 2:
创建两个独立的布局,设计相同但布局不同。
将其中一个设置为constraintLayoutturquoise,创建一个ImageView,将另一个设置为白色,并根据需要设置marginTop

欢迎提出任何问题。

英文:

there is two solutions:

Solution 1:
apply corners and padding to the rectangle with color turquoise

&lt;layer-list xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
    &lt;item&gt;
        &lt;shape android:shape=&quot;rectangle&quot;&gt;
            &lt;solid android:color=&quot;@color/turquoise&quot; /&gt;
            &lt;padding android:top=&quot;28dp&quot; /&gt;
            &lt;corners
                android:topLeftRadius=&quot;14dp&quot;
                android:topRightRadius=&quot;14dp&quot; /&gt;
        &lt;/shape&gt;
    &lt;/item&gt;
    &lt;item&gt;
        &lt;shape android:shape=&quot;rectangle&quot;&gt;
            &lt;corners
                android:bottomLeftRadius=&quot;0dp&quot;
                android:bottomRightRadius=&quot;0dp&quot;
                android:topLeftRadius=&quot;14dp&quot;
                android:topRightRadius=&quot;14dp&quot; /&gt;
            &lt;padding
                android:bottom=&quot;5dp&quot;
                android:left=&quot;5dp&quot;
                android:right=&quot;5dp&quot;
                android:top=&quot;5dp&quot; /&gt;
            &lt;solid
                android:color=&quot;@color/white&quot;
                android:elevation=&quot;0dp&quot; /&gt;
            &lt;size android:height=&quot;60dp&quot; /&gt;
        &lt;/shape&gt;
    &lt;/item&gt;
&lt;/layer-list&gt;

please note that android:top value u have to set depending on your needs.

Solution 2:

create two separated layouts with the same designs just differ in layout.
set one with turquoise to constraintLayout and crate ImageView to have the one with white and set marginTop depending on your needs

any questions are welcomed

huangapple
  • 本文由 发表于 2020年1月6日 22:07:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613559.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定