2012-11-24 46 views
0

我想在佈局中填充任意寬度和相同高度的兩個圖像(填充設備寬度)。在佈局中組合2個圖像

這個例子大小:

  • 骰子:427x427
  • 多米諾:900x427

鑑於與Domino以外的圖像和骰子的目標可能是這樣的:

enter image description here

我的初始代碼是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#999999" 
     android:orientation="horizontal" > 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:adjustViewBounds="true" 
      android:src="@drawable/domino" /> 

     <ImageView 
      android:id="@+id/imageView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:adjustViewBounds="true" 
      android:src="@drawable/dice" /> 

    </LinearLayout> 

對於XLARGE屏幕的結果是:

enter image description here

而對於小屏幕的多米諾通吃的寬度和骰子甚至不出現。

還試圖將兩個圖像的權重設置爲1,但結果也是錯誤的,並根據屏幕大小而變化。

我該如何解決這個問題?謝謝!

+0

你可以嘗試設置骰子的重量爲427,多米諾骨牌的重量爲900。 – yDelouis

+0

試過了,配置這兩個圖像有相同的寬度(每個屏幕的一半) – Addev

+0

嗨,只是一個建議,在你的情況下,如果你使用weight屬性,你必須設置寬度爲0,否則重量不起作用。你把它設置爲0嗎? – kameny

回答

1

看看這是你想要的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#999999" > 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/dice" /> 

    <ImageView 
     android:id="@id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@id/imageView2" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@id/imageView2" 
     android:src="@drawable/domino" /> 

</RelativeLayout> 

您可能需要爲ImageViews爲「拉伸」的圖像android:scaleType屬性。

+0

它對於佈局大小很好,但我也需要保持寬高比=( – Addev

+0

@Addev「ImageViews」中圖像的縱橫比?您是否嘗試過使用'scaleType'屬性(將'fitXY'作爲值如果我沒錯的話)? – Luksprog