2012-03-13 129 views
1

在我的应用程序中,我有一个按钮,当单击将新图像加载到图像视图时。问题是,当它这样做时,按钮上的文字改变方向。单击按钮后Android按钮文本更改为垂直

我的继承人该类

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> 

这里有问题的照片

When app first loads

After clicking Next

+0

已更新帖子以包含图片 – mpeerman 2012-03-13 08:08:00

+0

您是否也可以分享布局? – Soham 2012-03-13 08:11:11

+0

好吧,我在你的第二个表格布局添加的布局,以及 – mpeerman 2012-03-13 08:14:09

回答

3

与修改试试这个。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 
      <Button 
       android:id="@+id/dwnld" 
       android:text="Download" 
       android:layout_margin="2dp" 
       android:textColor="#000000" 
       android:layout_weight="0.5" 
       android:layout_width="0dp" 
       android:layout_height="30dp"/> 
      <Button 
       android:id="@+id/next" 
       android:text="Next" 
       android:layout_margin="2dp" 
       android:textColor="#000000" 
       android:layout_weight="0.5" 
       android:layout_width="0dp" 
       android:layout_height="30dp"/> 
     </LinearLayout> 
     <ImageView android:id="@+id/imageView" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 

</LinearLayout> 
+0

除了必须将按钮的layout_height更改为wrap_content以外,还有其他完美之处。非常感谢。你能向我解释这个变化吗? – mpeerman 2012-03-13 08:32:36

+0

在任何情况下,30dp是标准尺寸,但它很好的解决了你的问题。我不太了解它,因为我对android也很陌生。是的,但我知道在那些我们有很多东西可以放置在单一布局中的地方应该使用相当多的布局。 – Android 2012-03-13 08:36:18

+0

30dp虽然它削减了我的部分文本。当设置为wrap_content时,它确保按钮对于文本足够高。 – mpeerman 2012-03-13 08:38:52