任何人都可以帮助我在这里我需要创建一个动态的ImageButton但我似乎无法让这个常规按钮工作,我在这一行上收到错误ll.addView(button,param);
package org.iimed.www;
import org.iimed.www.R.layout;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
public class Penicillins extends Activity implements OnClickListener {
ImageButton back,addmed;
Context adaba;
Drawable mmimed;
public void onCreate(Bundle SavedInstanceState) {
super.onCreate(SavedInstanceState);
setContentView(R.layout.penicillin);
back = (ImageButton) findViewById(R.id.back);
addmed = (ImageButton) findViewById(R.id.addmed);
back.setOnClickListener(this);
addmed.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.back:
startActivity(new Intent(
Penicillins.this, ImageTextListViewActivity.class));
break;
case R.id.addmed:
RelativeLayout ll=(RelativeLayout)findViewById(R.id.sundayopen);
setContentView(R.layout.sundayopen);
Button button= new Button (this);
LayoutParams param = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.addView(button,param);
}
}
错误:
01-07 18:49:59.299: E/AndroidRuntime(27389): at org.iimed.www.Penicillins.onClick(Penicillins.java:63)
答案 0 :(得分:0)
变化:
Button button= new Button (this);
LayoutParams param = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.addView(button,param);
到
Button button= new Button (this);
LayoutParams param = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
button.setLayoutParams(param);
ll.addView(button);
答案 1 :(得分:0)
使用RelativeLayout.LayoutParams.WRAP_CONTENT
代替LayoutParams.WRAP_CONTENT
和删除此setContentView(R.layout.sundayopen);
行。
因为这在这里毫无意义。
最后在break;
ll.addView(button,param);
尝试更改
LayoutParams param = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
要强>
RelativeLayout param = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);