2012-03-13 159 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