2014-01-21 134 views
0

我使用match_parent作爲高度,但我不想使用wrap_contentfill_parent作爲寬度。我想將高度設置爲與寬度相同。我如何在layout.xml中做到這一點?Android佈局高度和寬度

+0

我不確定你是怎麼做到的,但嘗試這樣的最好的方法是使用'Design'窗格玩弄它,看看有什麼作用。 –

+0

在dp中給出尺寸 –

+0

不要以爲你可以在xml中做到這一點。你最好的選擇是以編程的方式做到這一點。 –

回答

0

你的問題在很大程度上取決於你是否在肖像或風景。

如果您試圖使縱向的佈局寬度與縱向高度相匹配,那麼您的佈局將會脫離屏幕,這在xml中似乎不可行。反之亦然,將高度與橫向寬度進行匹配將使佈局也離開屏幕。

如果你試圖讓一個正方形佈局相同尺寸屏幕的最小尺寸,然後周圍的方式來做到這一點在XML是使一個形狀繪製的是正方形的,透明的和大於屏幕尺寸。然後將drawable設置爲relativelayout中的imageview,並將寬度和高度設置爲適當的設置(match_parent或wrap_content)。然後根據需要將相關視圖添加到相關佈局,並將它們引用到父級而不是圖像,從而使新視圖位於imageview的「頂部」。

在肖像,下面RelativeLayout的是方:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/white" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:adjustViewBounds="true" 
      android:src="@drawable/square_draw" /> 

    </RelativeLayout> 

</LinearLayout> 

在景觀,以下是RelativeLayout的方:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@android:color/white" 
     android:orientation="vertical" > 

     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" > 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_centerHorizontal="true" 
       android:layout_centerVertical="true" 
       android:adjustViewBounds="true" 
       android:src="@drawable/square_draw" /> 

     </RelativeLayout> 

    </LinearLayout> 

同時,確保adjustViewBounds設置爲true。出於某種原因,此解決方案不允許圖像比屏幕尺寸變大,並且由於透支而使用更多資源。