尝试在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"/> ...

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,它对它们中的任何一个都不起作用。

chifengbin 回答:尝试在activity_main以外的另一个布局文件夹中更改ImageButton的src

我找到了在 MainActivity.ktactivity_main.xmlcontent_main.xml(包含布局)中定义属性的解决方案。这里的关键词是DataBinding。该项目是完全可重现的,我提供第一个 Kotlin,最后提供 JAVA 文件。


科特林:

要启用 DataBinding,您需要转到 build.gradle(Module) 并添加以下代码:

//...
dataBinding{
        enabled true
    }
//...

您将名为 DrawableContainer 的容器定义为 Kotlin class。在那里您定义了一个名为 DrawablecustomDrawable

因此DrawableContainer.kt

import android.graphics.drawable.Drawable

data class DrawableContainer(val customDrawable: Drawable)

现在我们将定义我们的 MainActivity.kt,它将绑定我们选择的 Drawable 并将其传递到我们的容器 (DrawableContainer)。

我们的MainActivity.kt

import android.app.Activity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.example.imagebuttonexperiment.databinding.ActivityMainBinding

class MainActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        val binding: ActivityMainBinding = DataBindingUtil.setContentView(
            this,R.layout.activity_main)
        binding.drawable = DrawableContainer(resources.getDrawable(R.drawable.my_image))

    }
}

缺少的部分是我们的 XML 文件。下面的代码显示了我们的 content_main.xml。它包含一个 variable(在 <data> 中),我们将定义名为 drawabletype 指南 DrawableContainer。因此,这是我们将<include 连接到容器和布局之间的第一座桥梁。在 ImageButton 中,您可以看到作为 android:src,我们将 variable 引用到容器中的 Drawable。这就是为什么android:src="@{drawable.customDrawable}"

因此content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android">
    
    <data>
        <variable
            name="drawable"
            type="com.example.imagebuttonexperiment.DrawableContainer" />
    </data>

    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:src="@{drawable.customDrawable}"/>
</layout>

现在建造我们的第二座桥很重要。然而,我们有 DrawableContainer -> content_main。这将是桥 content_main -> MainActivity。因此,我们再次定义了 <data/>variable。正如您在 <include 中所见,我们 bind:drawable 完全相同的 variable 位于两个 XML 文件中。 我们拼图中缺失的部分是 activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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.imagebuttonexperiment.DrawableContainer" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <include
            android:id="@+id/include_content"
            layout="@layout/content_main"
            bind:drawable = "@{drawable}"/>
    </LinearLayout>
</layout>

结果:

Result

最终结果是 ImageButton 中的一个 MainActivity 被另一个 Layout 包含。但是 Drawable 是从 MainActivity 中选择的,并传递了一个要显示在包含的 Layout 中的容器。是不是魔法!? ;)


JAVA:

我会信守承诺,并为您提供JAVA版本。 XML 文件和 gradle 将相同,您只需要使用 MainActivity.java 而不是 MainActivity.ktDrawableContainer.java 而不是 {{1} }.

这里是绿色的,DrawableContainer.kt

DrawableContainer.java

当然还有我们的import android.graphics.drawable.Drawable; public class DrawableContainer { public final Drawable customDrawable; public DrawableContainer(Drawable customDrawable) { this.customDrawable = customDrawable; } }

MainActivity.java

下载项目:

此外,我为项目提供了 import androidx.appcompat.app.AppCompatActivity; import androidx.databinding.DataBindingUtil; import android.annotation.SuppressLint; import android.graphics.drawable.Drawable; import android.os.Bundle; import com.example.imagebuttonexperiment.databinding.ActivityMainBinding; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityMainBinding mainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main); mainBinding.setDrawable(new DrawableContainer(getDrawable(R.drawable.my_image))); } } JAVA 类。您只需要评论您不想要的 2 个灰色类的内容。例如,如果您想使用 Kotlin,请将 Kotlin 文件的内容变灰。

Github Project


文档和教程:

,

@DEX7RA 注意:与 DEX7RA 解决方案相关的问题。不是解决方案的答案。 现在我收到这些错误:enter image description here

我与您的代码的唯一区别是 我添加了这个 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}"/>...
,

注意:这有点解决了我的问题: 我试过这个:

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

    colorDialog2=Dialog(this)
    colorDialog2.setContentView(R.layout.dialog_colors)
    colorDialog2.setTitle("Colors")

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

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

}

而不是在 Main.Activity 中

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)}

它奏效了,我设法根据需要更改了 src。 (仅此更改没有执行其他更改提供的更改)但是,我什至不确定这是否是一个好的做法。它确实可以作为快速修复。 稍后我会详细研究 DEX7RA 的回答。

本文链接:https://www.f2er.com/46687.html

大家都在问