更改Imagebutton的背景图片

时间:2013-12-23 08:49:57

标签: android xml background imagebutton selected

我很抱歉,如果之前已经发布过,或者可能没有,但我遇到了几个图像按钮的问题,当我按下它们时它不会改变图像。我使用了与我的标签一样的逻辑,我调用了x.xml(x是一个示例名称)文件,该文件存储在我的drawable文件夹中,并从相应的图像按钮调用该文件夹。

这是更改我使用的图像按钮的代码(drawable / dbmdtndeck.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">


<!-- When selected -->
<item android:drawable="@drawable/ydeck2" 
      android:state_selected="true"
      android:state_focused="true" 
      android:state_pressed="true" />


<!-- When not selected -->
<item android:drawable="@drawable/ydeck"  />


</selector>

这是我使用的文件存储在布局文件夹(layout / frmdbm.xml)中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dbmbackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<ScrollView
    android:id="@+id/dbmscrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/dbmllbackground1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

<LinearLayout
    android:id="@+id/dbmllbackground2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/imgbtnedit"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:background="@drawable/dbmbtnedit"
        android:scaleType="fitXY" />

    <LinearLayout
        android:id="@+id/dbmllblankspace2"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_alignParentBottom="true"
        android:layout_weight="1"
        android:orientation="horizontal" >
    </LinearLayout>

    <ImageButton
        android:id="@+id/imgbtndeck"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:background="@drawable/dbmbtndeck" />

</LinearLayout>

<LinearLayout
    android:id="@+id/dbmllblankspacemid"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:orientation="vertical" >
    <LinearLayout
        android:id="@+id/dbmllbackground6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <LinearLayout
        android:id="@+id/dbmllblankspace4"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:orientation="horizontal" >
    </LinearLayout>
    <ImageButton
        android:id="@+id/imgbtnsearch"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:background="@drawable/dbmbtnsearch" />

    <LinearLayout
        android:id="@+id/dbmllblankspace5"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:orientation="horizontal" >
    </LinearLayout>
  </LinearLayout>
</LinearLayout>

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

    <ImageButton
        android:id="@+id/imgbtnwish"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:background="@drawable/dbmbtnwish"
        android:scaleType="fitXY" />

    <LinearLayout
        android:id="@+id/dbmllblankspace"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_alignParentBottom="true"
        android:layout_weight="1"
        android:orientation="horizontal" >
    </LinearLayout>

    <ImageButton
        android:id="@+id/imgbtnrestrict"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:background="@drawable/dbmbtnrestrict" />
    </LinearLayout>

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

这是我的java类代码(dbm.java):

package com.example.yao;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
/* FOR TOAST (POP MESSAGE),  BUTTON,  AND IMAGE BUTTON*/
import android.app.AlertDialog;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.Toast;
import android.widget.ImageButton;
/*   FOR TAB RESOUCE */
import android.app.TabActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class dbm extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frmdbm);
      /* Calling TOAST MESSAGE
       * 
       * 
       */ 
       addListenerOnButton();

    }

   /* TOAST MESSAGE (POP MESSAGE)
    * 
    */
    public void addListenerOnButton() {

    /*
     * ASSINING OBJECTS TO BUTTON VERIABLES
     */

    ImageButton imageButtonedit = (ImageButton) findViewById(R.id.imgbtnedit);
    ImageButton imageButtondeck = (ImageButton) findViewById(R.id.imgbtndeck);
    ImageButton imageButtonwish = (ImageButton) findViewById(R.id.imgbtnwish);
    ImageButton imageButtonsearch = (ImageButton) findViewById(R.id.imgbtnsearch);
    ImageButton imageButtonrestrict = (ImageButton) findViewById(R.id.imgbtnrestrict);

    /*
     * WHEN CLICK ON THE OBJECT POP MESSAGE APPEARS
     */

    imageButtonedit.setOnClickListener(new OnClickListener()
    {   
        @Override
        public void onClick(View arg0) 
        {
           Toast.makeText(dbm.this,
            "DECK EDITOR", Toast.LENGTH_SHORT).show();
        }   
    });

    imageButtondeck.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0) 
        {
           Toast.makeText(dbm.this,
            "DECK RECEPICES", Toast.LENGTH_SHORT).show();
        }
    });

    imageButtonwish.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0) 
        {
           Toast.makeText(dbm.this,
            "WISH LIST", Toast.LENGTH_SHORT).show();
        }
    });

    imageButtonsearch.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0) 
        {
           Toast.makeText(dbm.this,
            "CARD SEARCHER", Toast.LENGTH_SHORT).show();        
        } 
    });

    imageButtonrestrict.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0) 
        {
           Toast.makeText(dbm.this,
            "FORBIDDEN / LIMITED LIST", Toast.LENGTH_SHORT).show();   
        }
    });

    }
}

此外,我需要说这个布局(frmdbm.xml)被调用到一个framelayout,我在我的activity_main.xml中调用了tabcontent。当用户使用相应的选项卡时,该布局(frmdbm.xml)将出现在framelayout中。另外,我没有得到任何错误,但我注意到它没有改变图像,它保留原始图像。我是这个Android开发人员的新手,我不确定是怎么回事,因为我对我的标签做了这个,它似乎工作得很好。不知道我在这里做了什么不同。

谢谢

3 个答案:

答案 0 :(得分:1)

使用此:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/button_idle"
        android:state_focused="false"
        android:state_pressed="false"
        android:state_selected="false"/>
    <item android:drawable="@drawable/button_pressed"
        android:state_focused="false"
        android:state_pressed="true"
        android:state_selected="false"/>

</selector>

答案 1 :(得分:0)

尝试将此作为选择器......

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">    
    <item android:state_selected="true" android:drawable="@drawable/learn" />
    <item android:drawable="@drawable/learng" />
</selector>

答案 2 :(得分:0)

试试..

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_selected="true" android:drawable="@drawable/buttonpress"/>
    <item android:state_focused="true" android:drawable="@drawable/buttonpress"/>
    <item android:state_pressed="true" android:drawable="@drawable/buttonpress"/>
    <item android:drawable="@drawable/button"/>

</selector>