2010-12-23 63 views
0

以下是我的代碼。我想要一個界面,我有一個單行文本框,一個多行文本框,下面有兩個按鈕。我希望多行文本框在渲染按鈕和文本框後佔用所有可用空間。爲此,我在主佈局中創建了兩個LinearLayouts。第一個垂直方向的layout_width設置爲fill_parent。第二個是水平的fill_parent。Android - 線性佈局排列,未按預期定位

第一個有一個文本框,我已將layout_height設置爲填充父項。第二個有兩個文本框確定並取消。

當我運行這個應用程序,我得到的用戶界面,但按鈕是非常小(約5px的高度)。我必須手動設置按鈕高度。我在這裏做錯了什麼。我不想硬編碼按鈕高度。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> 
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> 
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Name"></TextView> 
    <EditText android:layout_width="fill_parent" android:layout_height="wrap_content"></EditText> 
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Contents"></TextView> 
    <EditText android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="top" /> 
</LinearLayout> 
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"> 
    <Button android:id="@+id/okbutt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="OK" android:layout_weight="1" /> 
    <Button android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="Cancel" android:layout_weight="1" /> 
</LinearLayout> 

感謝, 阿倫

回答

2

我很好奇如何做到這一點,所以我決定嘗試一下。這是我想出了:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
    android:id="@+id/buttons" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true"> 
    <Button 
     android:id="@+id/okbutt" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="OK" /> 
    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Cancel" /> 
    </LinearLayout> 
    <TextView 
    android:id="@+id/textview1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:text="Name" /> 
    <EditText 
    android:id="@+id/textedit1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/textview1" /> 
    <TextView 
    android:id="@+id/textview2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/textedit1" 
    android:text="Contents" /> 
    <EditText 
    android:id="@+id/textedit2" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@id/textview2" 
    android:layout_above="@id/buttons" /> 
</RelativeLayout> 

它似乎做你想做的。

+0

感謝svdree,您的解決方案的工作原理。仍然想知道爲什麼我沒有。 – Arun 2010-12-23 13:57:16

0

可以使用相對佈局在這裏,而不是兩個線性佈局,我認爲這將給予更清晰的用戶界面。和
如果u想增加按鈕的高度手動只是增加文字寫在按鈕的高度,OK即高度與

安卓取消:TEXTSIZE

試試這個,可能是這樣的一個幫助你。