2011-10-11 49 views
0

我想下面佈局的活動:android佈局:如何寫這個?

top button 
middle(scroll view) 
bottom button 

我想底部按鈕連接到活動視圖的底部,中間部分佔據的空間的其餘部分。

我如何寫佈局xml文件?

下面不工作:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <LinearLayout android:focusable="true" 
     android:focusableInTouchMode="true" android:layout_width="0px" 
     android:layout_height="0px" /> 

    <LinearLayout android:orientation="vertical" 
     android:layout_width="fill_parent" android:weightSum="1" android:layout_height="wrap_content"> 

     <LinearLayout android:orientation="horizontal" 
      android:layout_width="fill_parent" android:layout_height="wrap_content" 
      android:weightSum="1"> 
      <EditText android:id="@+id/edit_search" 
       android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:lines="1" android:hint="發微博內容" android:layout_weight="0.9" /> 
      <Button android:layout_height="wrap_content" android:text="Search" 
       android:layout_width="wrap_content" android:id="@+id/search_btn" 
       android:layout_weight="0.1"></Button> 
     </LinearLayout> 

     <TextView android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:id="@+id/TextView01" 
      android:paddingTop="5dip" /> 
     <ListView android:layout_width="fill_parent" 
      android:id="@+id/lv01" android:layout_height="390dp"> 
     </ListView> 
     <Button android:id="@+id/create_new_btn" 
     android:layout_height="wrap_content" android:text="我要評論" android:layout_gravity="bottom" 
     android:layout_width="fill_parent"></Button> 
    </LinearLayout> 

</LinearLayout> 

回答

2

對於這種情況,只取RelativeLayout的,其對於這種情況更好,你不需要採取任何子佈局。

只需嘗試一次,並在此處發佈XML佈局以進一步改進。

從你的XML佈局代碼,我可以假設,你只是想有:

的EditText +按鈕=>頂, 的TextView +的ListView =>中東, 新建按鈕=>底部,

是這種情況?

+0

是的,這是事實。 –

0
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"></Button> 
    <Button android:id="@+id/button2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true"></Button> 
    <ScrollView android:id="@+id/scrollView1" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_below="@+id/button1" android:layout_above="@+id/button2"> 
     <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent"></LinearLayout> 
    </ScrollView>   
</RelativeLayout> 

我認爲類似的東西會起作用。該佈局中的重要部分是android:layout_belowandroid:layout_above。您可以用其他佈局(如ListView)替換ScrollView,並且它將保持在第一個按鈕之下並且高於第二個按鈕,而不管其大小如何。同樣,這些按鈕的屬性android:layout_alignParentTop="true"android:layout_alignParentBottom="true"將分別保持在RelativeView的頂部和底部。

0

保持簡單,順應LinearLayout中,發揮與權重:

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0"> 
    </Button> 
    <ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1"> 
    <!-- Stuff in scroll view --> 
    </ScrollView> 
    <Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0"> 
    </Button> 
</LinearLayout> 
0

@ParashMayani是正確的..

下面是執行:

<LinearLayout android:focusable="true" 
    android:focusableInTouchMode="true" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:id="@+id/relLayout" android:orientation="horizontal" > 



     <EditText android:id="@+id/edit_search" 
      android:layout_width="wrap_content" android:layout_height="wrap_content" 
      android:lines="1" android:hint="Hello" android:layout_weight="0.7"/> 

      <Button android:layout_height="wrap_content" android:text="Search" 
      android:layout_weight="0.3" android:layout_width="wrap_content" android:id="@+id/search_btn" /> 

    </LinearLayout> 

    <TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:id="@+id/TextView01" 
     android:paddingTop="5dip" android:layout_below="@id/relLayout"/> 

     <Button android:id="@+id/create_new_btn" 
    android:layout_height="wrap_content" android:text="我要評論" android:layout_gravity="bottom" 
    android:layout_width="fill_parent" android:layout_alignParentBottom="true"/> 


    <ListView android:layout_width="fill_parent" 
     android:id="@+id/lv01" android:layout_height="0dp" android:layout_above="@id/create_new_btn" android:layout_below="@id/TextView01" /> 

+0

@Bin Chen:這個工作適合你嗎? –