首页 > 解决方案 > 尝试在 activity_main 以外的另一个布局文件夹中更改 ImageButton 的 src

问题描述

总结一下,我正在尝试从 MainActivity 更改我的 dialog_colors.xml 文件中的 ImageButtons 的 src。但是无论我做什么,我都无法改变它。我在我的 activity_main.xml 中使用 ImageButtons 尝试了相同的代码,但它不适用于 dialog_colors.xml 文件中的按钮。

activity_main.xlm

<androidx.constraintlayout.widget.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"
tools:context=".MainActivity">

<include
    android:id="@+id/dialog_colors"
    layout="@layout/dialog_colors"/> ...

对话框颜色.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<LinearLayout
    android:id="@+id/ll_paint_colors"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/red"
        android:layout_width="40sp"
        android:layout_height="40sp"
        android:src="@drawable/pallet_normal"
        android:background="@color/Red"
        android:layout_margin="1sp"
        android:tag="redTag"
        android:clickable="true"
        android:onClick="selectColor"

        />...

MainActivity.kt

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

    colorDialog=findViewById<LinearLayout>(R.id.dialog_colors)
    firstRowColors=colorDialog.findViewById<LinearLayout>(R.id.ll_paint_colors)
    secondRowColors=colorDialog.findViewById<LinearLayout>(R.id.ll_paint_colors2)
    drawingView=findViewById<DrawingView>(R.id.drawingView)

    pressedColor=secondRowColors[0] as ImageButton
    pressedColor!!.setImageResource(R.drawable.pallet_pressed)

}...

我也用 TextViews 等尝试过同样的事情。似乎我无法更改 dialog_colors.xml 文件中的任何内容。

不要对 firstRows、secondRows 等感到困惑,有很多 ImageButtons,它对它们中的任何一个都不起作用。

标签: androidxmlkotlinlayoutmobile-development

解决方案


@DEX7RA 注意:与 DEX7RA 解决方案相关的问题。不是一个解决方案的答案。现在我收到这些错误:在此处输入图像描述

我与你的代码的唯一区别是我添加了这个 intead 因为我认为我的版本不接受 urs:

buildFeatures{
    dataBinding = true
}

与您给出的示例相比,我的 activitymain.xml 是这样的:

<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">

<data>
    <variable
        name="drawable"
        type="com.example.drawingapp.DrawableContainer2" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <include
        android:id="@+id/include_content"
        layout="@layout/dialog_colors"
        bind:drawable = "@{drawable}"/>...

推荐阅读