android - 在运行时更改图像按钮
问题描述
我在工具栏上有一个 imagebutton 作为 img1 单击它会显示包含 imagebuttons 作为 img2、img3、img4 等的线性布局。现在,当我单击 img2 时,工具栏上的 img1 按钮应更改为 img2。我怎样才能做到这一点?我在clickListener上尝试了几个不同的步骤,但没有奏效..
任何帮助,将不胜感激?
我已经搜索过这个问题的答案,我知道如何将一个图像设置为另一个图像,但我想设置用户是否从线性布局中选择任何按钮。谢谢
在 Activity 类中,我有这段代码
private void showHideAnnotatePanel() {
ImageButton im = (ImageButton)findViewById(R.id.button_pen);
if (annotatePanel == null) {
annotatePanel = new AnnotatePanel(this);
LinearLayout ll = (LinearLayout)findViewById(R.id.toolbarLayout);
ll.addView(annotatePanel, 0);
ll.setVisibility(View.VISIBLE);
this.annotateShowing = true;
if (im != null) {
im.setImageResource(R.drawable.paintpalette_64_wmblue);
}
}
else if (annotateShowing == false) {
LinearLayout ll = (LinearLayout)findViewById(R.id.toolbarLayout);
ll.setVisibility(View.VISIBLE);
annotateShowing = true;
if (im != null) {
im.setImageResource(R.drawable.paintpalette_64_wmblue);
}
}
else {
LinearLayout ll = (LinearLayout)findViewById(R.id.toolbarLayout);
ll.setVisibility(View.GONE);
annotateShowing = false;
if (im != null) {
im.setImageResource(R.drawable.brush_icon_selector);
}
}
}
the new_annotate_screen.xml file is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#323232"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:background="#323232"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/col_one"
android:layout_width="305dp"
android:layout_height="50dip"
android:orientation="horizontal" >
<!-- -->
<ImageButton
android:id="@+id/new_red_col"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_margin="5dip"
android:background="#ff0000" />
<ImageButton
android:id="@+id/new_green_col"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_margin="5dip"
android:background="#00ff00" />
<ImageButton
android:id="@+id/new_blue_col"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_margin="5dip"
android:background="#0000ff" />
<ImageButton
android:id="@+id/new_yellow_col"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_margin="5dip"
android:background="#ffff00" />
</LinearLayout>
</LinearLayout>
解决方案
//considering R.drawable.paintpalette_64_wmblue is the img2
img2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
img1.setImageResource(R.drawable.paintpalette_64_wmblue);
}
});
推荐阅读
- python - python win32com设置office 365 excel用3张打开
- sql - 在 pyspark 中使用 col 函数查看所有列
- servicenow - 如何找到写入日志的脚本
- android - 我的布局可见性功能不起作用
- c# - 在 C# foreach 中汇总结果
- xml - XSLT 解析工作错误
- r - 如果特定列的值完全丢失,如何在 R 中添加 NA 行?
- linux - 如何编译 glibc(32 位和 64 位)?
- angular - 来自父窗口的 Angular iframe 调整大小事件陷阱
- java - 将游戏迁移到 2.8 时出现以下错误。请帮助了解任何与记录器相关的弃用