android - 如何将我的 dialogFragment 显示为圆角?

标签 android android-studio kotlin android-dialogfragment rounded-corners

您好,我尝试使用圆角。这张图是dialogFragment最后的情况:https://pasteboard.co/IuVANG7.jpg

正如您在图片中看到的,这并不是圆角。我看到了一些解决方案,但它是 java 代码。我无法将 java 代码准确地转换为 kotlin。 :) 这是我的代码:

主要 Activity :

package com.example.dialogfragment
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

    }


    fun showDialog(view: View) {

        var MyDialog = MyFragment()
        MyDialog.show(supportFragmentManager, "My Dialog")


    }
}

我的对话框.xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/radius"
    android:orientation="horizontal">


    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:text="Bu dialog penceresini beğendiniz mi?"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="16dp" />

    <Button
        android:id="@+id/btnEvet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btnHayir"
        android:layout_alignBottom="@+id/btnHayir"
        android:layout_marginStart="0dp"
        android:layout_marginBottom="0dp"
        android:layout_toEndOf="@+id/btnHayir"
        android:text="EVET"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/btnHayir"
        tools:layout_editor_absoluteY="54dp" />

    <Button
        android:id="@+id/btnHayir"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv"
        android:layout_alignParentStart="true"
        android:layout_marginStart="41dp"
        android:layout_marginTop="0dp"
        android:text="HAYIR"
        app:layout_constraintEnd_toStartOf="@+id/btnEvet"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="54dp" />
</RelativeLayout>

还有我的可绘制对象:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorAccent"
       />
    <corners android:radius="30dp" />
    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />
    </shape>

最佳答案

将这一行添加到DialogFragment onCreatView 并使 View 的背景透明。

 dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

关于android - 如何将我的 dialogFragment 显示为圆角?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57709170/

相关文章:

java - 每次我添加 google.android.gms :play-services-ads:18. 1.0 时 list 合并失败

java - Reddit OAuth2 API 循环重定向

android - 在透明的 ActionBar 后面部分显示 ImageView

java - MultiDex NoClassDefFounderror

android - 已启用 Multidex - 仍然无法合并 dex

android - 为 Assets 和资源文件夹指定其他路径-Android Studio

kotlin - 如何使用属性实现抽象getter

android - 如何从 kotlin 中的 ArrayList 中删除所有项目

android - Activity 泄露了最初在这里注册的 IntentReceiver。您是否错过了对 unregisterReceiver() 的调用?

java - 我在 OnResume() 而不是 OnCreate() 中有必要的代码,但是当我转到不同的 Activity 然后返回时,ListView 中的数组数据仍然消失