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

+0

レイアウトを共有することはできますか? – Soham

+0

私はレイアウトを追加しました。 – mpeerman

答えて

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

+0

いずれにしても30dpは標準サイズですが、問題が解決しました。私はアンドロイドにも新しいので、私はそれをあまり知らない。はい、私は知っています。単一のレイアウトで配置することが多い場所では、多くの相対レイアウトを使用する必要があります。 – Android

+0

私のテキストの一部を切っても30dp。 wrap_contentに設定すると、ボタンのテキストが十分に高いことが保証されます。 – mpeerman

関連する問題