问题描述
我在这里的目标是从的ImageButton(ibChamp)改变图像。
My goal here is to change an Image from an ImageButton(ibChamp).
package com.example.custombuilds;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;z
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class Champions extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.champions);
ImageButton ibAnnie = (ImageButton) findViewById(R.id.ibAnnie);
ibAnnie.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Check this for errors
RelativeLayout test = (RelativeLayout) findViewById(R.id.layoutChampions);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.create_build_page, null);
test.addView(view);
ImageButton img = (ImageButton) view.findViewById(R.id.ibChamp);
img.setImageResource(R.drawable.ic_launcher);
try {
Intent open = new Intent("android.intent.action.CREATE");
startActivity(open);
} catch (Exception e) {
}
}
});
}
}
注意的ImageButton ibChamp从XML布局create_build_page,而不是XML冠军。
这code运行没有崩溃,但是从的ImageButton ibChamp图像不会改变,这是我要做的。我会很乐意提供任何其他附加信息。
Note that the ImageButton ibChamp is from the xml layout create_build_page, and not the xml champions.This code runs without crashing, but the Image from the Imagebutton ibChamp does not change, which is what I am trying to do. I will be happy to provide any other additional information.
推荐答案
您在的onClick坐大ibChamp。这将创建一个新的ImageButton。你不会看到它,直到你用addView从父。
You are inflating "ibChamp" in onClick. That creates a new ImageButton. You will not see it until you use "addView" from the parent.
您可以将您的XML,但是你需要为了改变它才能到活动中现有按钮的引用。或者添加新的按钮,删除旧...
You can add your XML, but you need to get a reference to an existing button in Activity in order to change it. Or else add the new button and remove an old one...
在换句话说,改变这个
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.create_build_page, null);
ImageButton img = (ImageButton) view.findViewById(R.id.ibChamp);
这样:
ImageButton img = (ImageButton)findViewById(R.id.ibChamp);
和它可能会做你想要的。
And it will probably do what you want.
这篇关于对于ImageButton的图像没有变化如预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!