android对话框不会加载完整数据

标签 android android-layout kotlin

我有基于 imageButtons 打开的对话框我可以为每个 imageButton 打开我的对话框,但我的对话框不会显示完整数据。

问题

  1. 对话框中的按钮不会显示
  2. 我的对话框没有背景

代码

自定义对话框

<?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="match_parent">

    <ImageView
        android:id="@+id/dialog_imageview"
        android:layout_width="294dp"
        android:layout_height="246dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:layout_marginStart="68dp"
        android:layout_marginTop="274dp"
        android:layout_marginEnd="69dp"
        android:layout_marginBottom="270dp"
        android:contentDescription="image"
        android:foregroundGravity="center"
        tools:srcCompat="@tools:sample/avatars[0]" />

    <TextView
        android:id="@+id/tv_text"
        android:layout_width="wrap_content"
        android:layout_height="49dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="59dp"
        android:layout_marginTop="474dp"
        android:layout_marginEnd="59dp"
        android:layout_marginBottom="136dp"
        android:gravity="center_vertical"
        android:text="TextView"
        android:textAlignment="center"
        android:textSize="30sp" />

    <Button
        android:id="@+id/close_btn"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="285dp"
        android:layout_marginTop="680dp"
        android:layout_marginEnd="59dp"
        android:layout_marginBottom="193dp"
        android:background="#F50057"
        android:gravity="center"
        android:text="Close"
        android:textAlignment="center"
        android:textColor="#FFFFFF" />

    <Button
        android:id="@+id/play_btn"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="59dp"
        android:layout_marginTop="680dp"
        android:layout_marginEnd="289dp"
        android:layout_marginBottom="190dp"
        android:background="#00E676"
        android:gravity="center"
        android:text="Play"
        android:textAlignment="center" />
</RelativeLayout>

Activity 函数

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

        // open dialog1
        val imageButton1 = this.findViewById(R.id.imageButton1) as ImageButton;
        imageButton1.setOnClickListener() {
            val dialog = Dialog(context, android.R.style.Theme_Translucent_NoTitleBar)
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)
            dialog.getWindow().setGravity(Gravity.CENTER)
            dialog.setContentView(R.layout.dialog_custom)

            val tv_text = dialog.findViewById(R.id.tv_text)as TextView
            val btn_close = dialog.findViewById(R.id.close_btn) as Button
            val btn_play = dialog.findViewById(R.id.play_btn) as Button
            val imageView = dialog.findViewById(R.id.dialog_imageview) as ImageView

            imageView.setImageResource(R.drawable.school) //set image here
            tv_text.setText("School")  // set description here


            //insert your button function here
            btn_close.setOnClickListener {
                fun onClick(v: View) {
                    dialog.dismiss()
                }
            }

            btn_play.setOnClickListener {
                fun onClick(v: View) {
                    val mp: MediaPlayer? = MediaPlayer.create(this, R.raw.a)
                    mp?.start();
                }
            }

            dialog.show();
        }
}

截图

one

有什么想法吗?

最佳答案

不同的手机有不同的屏幕尺寸,在您的布局中,您在 View 上使用固定尺寸(例如,固定尺寸为 android:layout_marginTop="474dp")并且结果是在一个屏幕(您的 android studio 预览屏幕)上看起来不错的东西在另一个屏幕(您的实际手机)上看起来可能不太好。

如何修复它 - 您可以使用 ConstraintLayout让您的屏幕响应所有屏幕尺寸。

以此布局为例:

<?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="match_parent"
  android:layout_height="match_parent">


<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:text="Button"
    app:layout_constraintHeight_percent="0.1"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:text="Button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_percent="0.1"
    app:layout_constraintTop_toTopOf="@+id/guideline" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:scaleType="fitXY"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_percent="0.4"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:srcCompat="@tools:sample/avatars[6]" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:gravity="center"
    android:text="Your text here"
    app:layout_constraintBottom_toTopOf="@+id/guideline"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView" />

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.7" />
</android.support.constraint.ConstraintLayout>

它看起来像这样并且会响应所有屏幕尺寸:

enter image description here


这看起来像您想要的样子,但让我们解释一下我做了什么:

  • 我在按钮上使用了 app:layout_constraintHeight_percent="0.1" 来告诉它们的高度等于屏幕尺寸的 10%(不是固定尺寸)。

  • 基于相同的概念,我还使用了 app:layout_constraintHeight_percent="0.4" 因此图像将等于屏幕尺寸的 40%高度。

  • 我也用过Guidelines以便更好地控制按钮的位置。

您可以在 Support different screen sizes 中找到有关创建响应式布局的更多信息, Barriers如上所述Guidelines还有

使用上述工具,您可以创建响应式布局。 您不必使用 ConstraintLayout 来实现此目的,您还有其他选择,但我发现这是创建布局的最简单方法。

关于android对话框不会加载完整数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58238920/

相关文章:

java - Android-按钮滑动效果

android - 要存储在缓存中的图像数组?

android - 重命名变量以使 XML id 更清晰

kotlin - 如何跨多个异步源收集具有不变属性的kotlin数据类的数据?

Android手机在执行我自己开发的应用程序时重启

java - Android - 向图像添加 View 不会更新 - onClick

java - 如何在 Android 11 和 12 中获取 PDF 文件路径

android - 为什么在 Android 编程中 UI 不使用 XML 而不是 HTML?

android - 使用阴影创建自定义 ListView

android - 如何设法在自定义 View 组中将一个 child 带到其他 child 的前面?