在我的应用程序中,我有一个按钮,当点击将新图像加载到图像视图中时。问题在于,当它执行此操作时,按钮上的文本会改变方向。
这是本课程的代码
package com.michaelpeerman.demotivational_posters;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class ImageGallery extends Activity {
public ImageView i;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_gallery);
Button next = (Button) findViewById(R.id.next);
Button download = (Button) findViewById(R.id.download);
download.setOnClickListener(this.buttonhandler);
next.setOnClickListener(this.buttonhandler);
try {
int rand = 1 + new Random().nextInt(2368);
String url = "http://someurl.com/"+rand+".jpg";
i = (ImageView)findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
i.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
View.OnClickListener buttonhandler = new View.OnClickListener()
{
public void onClick(View v)
{
switch (v.getId())
{
case R.id.next:
loadImage();
break;
}
}
};
void loadImage(){
int rand = 1 + new Random().nextInt(2368);
String url = "http://someurl.com/"+rand+".jpg";
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
i.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
当应用程序首次使用第一个按钮加载时,文本正常。但是当我点击下一步时,按钮上的文字就会改变。
这也是布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableLayout
android:id="@+id/tablelayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableRow android:id="@+id/tableRow1"
android:layout_height="wrap_content">
<Button android:id="@+id/download"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:layout_weight=".45"
android:text="Download"/>
<Button android:id="@+id/next"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:layout_weight=".45"
android:text="Next"/>
</TableRow>
</TableLayout>
<TableRow android:id="@+id/tableRow2"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@id/tablelayout1">
<ImageView android:id="@+id/imageView" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
</RelativeLayout>
以下是问题的图片