2014-02-10 30 views
-1

將大於Galaxy Tab 2 P5100(運行4.1.2)寬度的圖像加載到ImageView中會爲加載的圖像添加某種頂部/底部填充。在Galaxy Tab 2上加載圖像時的佈局問題

下面是與Show layout boundaries截圖開啓:

wrong alignment

下面是它應該如何看(從Nexus 10的運行4.4.2):

correct alignment

我使用的代碼(對於上述兩個示例)是

public class ImageBugActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_bug); 

     // This bug is still reproducible if I use the 
     // Universal-Image-Loader library or if I change the dimensions of 
     // the image to a different width 
     loadImage("http://placehold.it/1600x1000", (ImageView)findViewById(R.id.image)); 
    } 

    private void loadImage(final String url, final ImageView view) { 
     new Thread(new Runnable() { 

      @Override 
      public void run() { 
       try { 
        final Bitmap bitmap = BitmapFactory.decodeStream(new URL(url).openConnection().getInputStream()); 
        runOnUiThread(new Runnable() { 

         @Override 
         public void run() { 
          view.setImageBitmap(bitmap); 
         } 
        }); 
       } catch (Exception e) { 
        Log.e("loadImage", e.getMessage(), e); 
       } 
      } 
     }).start(); 
    } 
} 

而且佈局文件是

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="On a Galaxy Tab 2 the image below it is pushed to the center of the remaining space." /> 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top" /> 

</LinearLayout> 

這個問題似乎是一個Android /三星的錯誤還是我做出愚蠢的錯誤?

+0

嘗試在系統設置 - >開發人員選項中將「顯示佈局邊界」選項設置爲true。然後打開應用程序並檢查邊界是 – Sam

+0

@ SamN-a:謝謝!它似乎沒有提出解決方案,但我更新了屏幕截圖。 – Nachi

+0

嘗試將佈局更改爲RelativeLayout。您必須添加一些其他修改,例如添加alignParentTop,layout_below,但它可能是值得的。目前我認爲這是一個錯誤,因爲這種行爲是出乎意料的 – Sam

回答

1

設置ImageView"fitStart"android:scaleType應該做的伎倆。

+0

這是訣竅,謝謝! – Nachi

-1

您應該使用ImageView的layout_height"match_parent"

<ImageView 
     android:id="@+id/image" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="top" /> 
+0

這不起作用。請注意,我上面發佈的代碼在Nexus 10設備上完美工作。 – Nachi