【问题标题】:Bottom Rounded Corners - Android底部圆角 - Android
【发布时间】:2019-10-23 15:28:05
【问题描述】:

我一直在尝试解决这个问题,但无论我做什么,我都无法按照我想要的方式获得底部圆角,我正在使用以下

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
      <shape android:shape="rectangle">
            <stroke android:width="0dp"  />
            <solid android:color="?attr/colorPrimaryDark"  />
      </shape>
   </item>
   <item android:top="0dp" android:bottom="1dp"> 
      <shape 
        android:shape="rectangle">
            <stroke android:width="0dp"/>
            <solid android:color="?attr/colorPrimary"/>
             <corners android:radius="15dp" android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp" android:topLeftRadius="0dp" android:topRightRadius="0dp"/> 
        </shape>
   </item>
</layer-list> 

结果如下

它创建了角落,但它添加了 灰色 (colorPrimaryDark),我希望它是白色 (colorPrimary") 并带有一条灰线 - 知道我是如何做到这一点的吗?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    可以通过一些小技巧来消除顶部和侧面的线条:设置负偏移量。

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item
                android:left="-1dp"
                android:right="-1dp"
                android:top="-1dp">
            <shape android:shape="rectangle">
                <stroke
                        android:width="1dp"
                        android:color="?attr/colorPrimaryDark" />
                <solid android:color="?attr/colorPrimary" />
                <corners
                        android:bottomLeftRadius="15dp"
                        android:bottomRightRadius="15dp"
                        android:radius="15dp"
                        android:topLeftRadius="0dp"
                        android:topRightRadius="0dp" />
            </shape>
        </item>
    
    </layer-list>
    

    不确定它是否能在所有设备/Android 版本上正常工作。

    【讨论】:

    • 嘿,Righto,我知道你在那里做了什么 :) 我必须做 android:top="-2dp" 才能让它在 OREO 和 PIE (API 28) 上不可见,我认为这会很棘手
    • 在 Pixel 3XL 上的 AndroidX 上具有高 DP,我不得不去 -3dp 使其不可见 - 我想没有实际的解决方案
    【解决方案2】:

    如果你只想要底部边框,Shape drawable 就足够了。您不需要layer-list。 试试这个

    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="1dp" android:color="@color/green"/>
    <solid android:color="?attr/colorPrimary" />
    <corners
            android:bottomLeftRadius="15dp"
            android:bottomRightRadius="15dp" />
    </shape>
    

    您当前代码的问题是第一项设置了纯色。这就是为什么显示灰色部分的原因。

    【讨论】:

    • 嘿,感谢您的回复,但这并没有划清界限 :) 在此处查看结果imgur.com/EgbNK6P
    • @Ali 将stroke 更改为&lt;stroke android:width="1dp" android:color="?attr/colorPrimaryDark" /&gt;
    • 不确定你的输出..因为我看不到全白的图像..我猜你需要A Card view with Bottom border only
    • @SergeyGlotov 如果您的意思是 &lt;stroke android:width="1dp" android:color="?attr/colorPrimaryDark" /&gt; 在顶部添加一行,请在此处查看结果 imgur.com/mR4Y1Ym
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多