英文:
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("Brush size: ")
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>```
</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("Brush size: ")
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论