为什么在使用 Kotlin Android Studio 中的对话框时,画笔大小选项不显示?

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

Why are the brush size options not appearing when using Dialogs in Kotlin Android Studio?

问题

当我输入 brushDialog.ib_small_brush应该出现选择小刷子的选项但它不起作用我尝试重建和清理项目但仍然不起作用

主活动函数
```kotlin
private fun showBrushSizeChooserDialog(){
    val brushDialog = Dialog(this)
    brushDialog.setContentView(R.layout.dialogbrushsize)
    brushDialog.setTitle("画笔大小:")
    val smallBtn = brushDialog
}

dialogbrushsize.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageButton
        android:id="@+id/ib_small_brush"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/small_brush"
        android:src="@drawable/small"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/ib_medium_brush"/>
    <ImageButton
        android:id="@+id/ib_medium_brush"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/medium_brush"
        android:src="@drawable/medium"
        app:layout_constraintTop_toBottomOf="@id/ib_small_brush"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/ib_large_brush"/>
    <ImageButton
        android:id="@+id/ib_large_brush"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/large_brush"
        android:src="@drawable/large"
        app:layout_constraintTop_toBottomOf="@id/ib_medium_brush"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
英文:

When i type brushDialog.ib_small_brush the option for the small brush should appear but it is not working. I tried rebuilding and cleaning the project but it still won't work.

an option of ib_large_brush, ib_medium_brush, and ib_small_brush should appear but it doesn't.

Main activity function:

private fun showBrushSizeChooserDialog(){
        val brushDialog = Dialog(this)
        brushDialog.setContentView(R.layout.dialogbrushsize)
        brushDialog.setTitle(&quot;Brush size: &quot;)
        val smallBtn = brushDialog
    }

dialogbrushsize.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;

    &lt;ImageButton
        android:id=&quot;@+id/ib_small_brush&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:contentDescription=&quot;@string/small_brush&quot;
        android:src=&quot;@drawable/small&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintBottom_toTopOf=&quot;@+id/ib_medium_brush&quot;/&gt;
    &lt;ImageButton
        android:id=&quot;@+id/ib_medium_brush&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:contentDescription=&quot;@string/medium_brush&quot;
        android:src=&quot;@drawable/medium&quot;
        app:layout_constraintTop_toBottomOf=&quot;@id/ib_small_brush&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintBottom_toTopOf=&quot;@+id/ib_large_brush&quot;/&gt;
    &lt;ImageButton
        android:id=&quot;@+id/ib_large_brush&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:contentDescription=&quot;@string/large_brush&quot;
        android:src=&quot;@drawable/large&quot;
        app:layout_constraintTop_toBottomOf=&quot;@id/ib_medium_brush&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;/&gt;




&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;```



</details>


# 答案1
**得分**: 0

这是因为 `Dialog` 不知道你的自定义布局以及它包含的小部件。

尝试像这样:
```kotlin
private fun showBrushSizeChooserDialog(){
  val brushDialog = Dialog(this)
  brushDialog.setContentView(R.layout.dialogbrushsize)
  brushDialog.setTitle("画笔大小:")

  val smallBtn = brushDialog.findViewById<ImageButton>(R.id.ib_small_brush)
  val mediumBtn = brushDialog.findViewById<ImageButton>(R.id.ib_medium_brush)
  val largeBtn = brushDialog.findViewById<ImageButton>(R.id.ib_large_brush)
}
英文:

That's because the Dialog doesn't know about your custom layout & the widgets it contains.

Try like this:

private fun showBrushSizeChooserDialog(){
  val brushDialog = Dialog(this)
  brushDialog.setContentView(R.layout.dialogbrushsize)
  brushDialog.setTitle(&quot;Brush size: &quot;)

  val smallBtn = brushDialog.findViewById&lt;ImageButton&gt;(R.id.ib_small_brush)
  val mediumBtn = brushDialog.findViewById&lt;ImageButton&gt;(R.id.ib_medium_brush)
  val largeBtn = brushDialog.findViewById&lt;ImageButton&gt;(R.id.ib_large_brush)
}

huangapple
  • 本文由 发表于 2023年6月1日 01:31:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376011.html
匿名

发表评论

匿名网友

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

确定