【问题标题】:Adding round corners to Android Layout leaves white edges [duplicate]向Android布局添加圆角会留下白边[重复]
【发布时间】:2019-01-22 20:17:45
【问题描述】:

我正在尝试创建一个带有圆角的Layout,我使用shape 来帮助我这样做,但是在添加圆角时,背景白色也会保留。

还需要注意的是,我将这个 layout.xml 用作 Dialog 的 ContentView:

final Dialog MyDialog = new Dialog(this);
MyDialog.setContentView(R.layout.layout.xml);

这是我的 layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="300dp"
    android:background="@drawable/round_corners"
    android:layout_height="200dp">

    <Button
        android:id="@+id/play_song"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="32dp"
        android:text="Play"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <Button
        android:id="@+id/delete_song"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginStart="28dp"
        android:text="Delete"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

和可绘制的round_corners.xml

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

这是我得到的结果:

如何去除白角?

【问题讨论】:

  • 从约束布局中取出样式参数,看看会出现什么。
  • 我忘了把它拿走,我只是在测试这个样式是否有帮助,但删除或添加它也没有任何作用
  • 尝试在代码中将对话框背景设置为透明.. !!

标签: java android xml constraints shape


【解决方案1】:

只需添加下面提到的代码行。它将为您的对话框设置透明背景。

MyDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

【讨论】:

  • 行得通,谢谢
  • 欢迎。祝你好运。
  • 非常感谢 :)
猜你喜欢
  • 2020-11-10
  • 2021-12-04
  • 2020-11-13
  • 1970-01-01
  • 1970-01-01
  • 2016-02-10
  • 1970-01-01
  • 1970-01-01
  • 2015-11-02
相关资源
最近更新 更多