android - 尝试在 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,它对它们中的任何一个都不起作用。
解决方案
@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}"/>...
推荐阅读
- reactjs - 在 ReactJS 链接中传递和访问参数
- excel - VBA删除活动工作表
- html - 为移动设备、桌面设备和打印设置字体大小的最佳方法是什么?
- pip - 在安装构建失败期间,“纯”Python + Cython 包会发生什么?
- javascript - ReactJS 使用 API 调用 - 每次使用组件时数据都会更改
- arrays - 如何在 ngModel 上传递数组列表?
- javascript - 为什么 Array.prototype.filter() 在 Magnolia JavaScript 模型中抛出错误?
- ios - 如何使用 UIScrollView 正确处理缩放图像上的移动?
- winforms - Winform:无法更新组合框
- javascript - 如何以 YYYY-MM-DD 格式显示输入日期