如果此問題已在之前發佈過,或者可能未發佈,但是我遇到了幾個圖像按鈕問題,並且它們在按下圖像時不會更改圖像,我很抱歉。我使用了與我的選項卡相同的邏輯,我使用x.xml(x是一個示例名稱)調用存儲在我的可繪製文件夾中的文件,並從相應的圖像按鈕中調用該文件夾。更改Imagebutton的背景圖像
這是變革的代碼,我用的ImageButton(繪製/ 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>
這裏是我使用存儲在佈局文件夾(佈局/ 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)被調用到我調用了activity_main.xml中的tabcontent的framelayout中。當用戶使用相應的選項卡並且該佈局(frmdbm.xml)將出現在framelayout中時。另外,我沒有得到任何錯誤,但我注意到它沒有改變圖像,它保留了原始圖像。我是這個android開發人員的新手,我不確定發生了什麼,因爲我對我的選項卡執行了這個操作,並且它看起來工作得很好。不知道我在這裏做了什麼不同。
謝謝
使用imagebuttonName。 setBac kgroundResource(R.drawable.yourimageName);在onclick –