在运行时更改imagebutton

时间:2018-07-23 10:00:11

标签: android

我在工具栏上单击时有一个imageimg,它显示为包含img2,img3,img4等的图像按钮的线性布局。现在,当我单击img2时,工具栏上的img1按钮应更改为img2。我该如何实现? 我尝试了几个不同的步骤onclickListener,但是没有用。

有什么帮助吗?

我已经搜索了这个问题的答案,我知道如何将一个图像设置为另一个图像,但是我想设置用户是否从线性布局中选择任何按钮。 谢谢

在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>

1 个答案:

答案 0 :(得分:0)

English
相关问题