请耐心等待,我是新来的android编程。我试图在android中点击按钮显示图像,但不幸的是我的模拟器没有显示所需的输出。这是我的layout.xml文件模拟器不显示按钮上的图像点击Android
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="14dp"
android:text="Button" />
</RelativeLayout>
main_activity.java
package com.example.app1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
image.setImageResource(R.drawable.optimus);
}
});
}
}
我已经列入/ RES /绘制-MDPI的图像文件。当我尝试在模拟器上运行时,它不会在按钮单击时显示图像。代码有问题吗?在使用它之前
Button theButton = (Button)findViewById(R.id.button1);
theButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
theButton.setBackgroundResource(R.drawable.optimus);
}
你甚至没有在你的Java文件中的按钮,更不用说一个点击监听器或图像 –
我没有得到你。请你可以编辑我的代码并指出我的错误。 Thx提前预测。 – Darpit
您的布局中没有** ImageView **。你如何假装把图像放在一个不存在的物体上?并且您也没有名为** textView1 **的控件**'android:layout_alignLeft =“@ + id/textView1”'或者我应该假定您**已经损坏了您的布局?你能完全展示**吗? –