2014-05-08 171 views
1

我在LinearLayout裏面有一個EditText標記,並且水平方向,我想讓我的EditText的高度爲父代LinearLayout's高度的百分比。這怎麼能實現?子元素的高度作爲父元素的百分比

請不要建議layout_weight屬性,因爲它將用於控制子元素的寬度而不是高度。

+0

我建議您創建一個自定義佈局,您將在其中處理孩子的尺寸,考慮創建您自己的屬性來設置百分比值。 – betorcs

回答

1

您可以編寫一個自定義佈局,將其子級調整爲正確的百分比,這可能是最佳解決方案。

或者你可以使用layout_weight和包裹EditText在另一個LinearLayout,是這樣的:一個的LinearLayout內

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:weightSum="2" 
     android:orientation="vertical"> 
     <View 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="1"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:text="one"/> 
    </LinearLayout> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="two"/> 
</LinearLayout> 
0

的百分比是很容易的。

給您的視圖的重量

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:weightSum="2" 
    android:orientation="vertical"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="0.5"/> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="0.25" 
     android:text="one"/> 
</LinearLayout> 

我設置兩個textViews的高度爲0,但layout_weight說做出的LinearLayout的一個50%,而其他25%的高度佈局。

相關問題