2010-10-15 126 views
1

我在layout.xml中有以下代碼。在線性佈局內嵌套相對佈局

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent"> 

    <EditText android:hint="@string/feed_url" 
     android:id="@+id/feedUrl" android:textSize="14dp" android:inputType="textUri" 
     android:layout_marginRight="45dp" android:layout_height="wrap_content" android:layout_width="fill_parent"> 
    </EditText> 

    <Button android:id="@+id/getGraph" 
     android:text="@string/get" 
     android:layout_toRightOf="@id/feedUrl" 
     android:layout_alignParentRight="true"   
     android:layout_height="wrap_content" android:width="45dp" android:layout_width="wrap_content"> 
    </Button> 

</RelativeLayout> 

<WebView android:id="@+id/wv1" android:layout_height="wrap_content" 
    android:layout_width="fill_parent" /> 
</LinearLayout> 

在eclipse插件佈局創建器中,EditText和按鈕顯示正確。 (參見下圖)

alt text

但是裝置和仿真器上時,按鈕未隱藏。 (見下面的截圖)

alt text

任何想法,爲什麼按鈕是越來越隱藏在設備?

回答

6

請嘗試以下更改代碼。您必須首先定義按鈕,因爲它的寬度是固定的,然後放置EditText以填充按鈕左側的其餘空間。該按鈕還必須先定義(在Android 2.2之前)。

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"> 

    <Button 
     android:id="@+id/getGraph" 
     android:text="@string/get" 
     android:layout_alignParentRight="true"   
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     /> 
    <EditText 
     android:hint="@string/feed_url" 
     android:id="@+id/feedUrl" 
     android:textSize="14dp" 
     android:inputType="textUri" 
     android:layout_marginRight="45dp" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_toLeftOf="@id/getGraph" 
     /> 
</RelativeLayout> 
+0

謝謝。奇蹟般有效。 – Sudar 2010-10-16 10:23:14

0

我看不到佈局構建器中的按鈕......原因是,我認爲寬度是EditText填充父項,所以它填充父按鈕出按鈕。如果您必須使用RelativeLayout,則需要明確定義EditText的寬度。否則,您可能想要切換到水平LinearLayout並將編輯框的權重設置爲「1」。

另外,還有一些無效的選項RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal 
0

您也可以使用LinearLayout而不是RelativeLayout來存檔它。您可以使用android:layout_weight使Button不被EditText覆蓋。以下是可能工作的示例xml:

<LinearLayout android:orientation="horizontal" 
    android:layout_height="wrap_content" android:layout_width="fill_parent"> 
    <EditText android:hint="@string/feed_url" 
     android:id="@+id/feedUrl" android:textSize="14dp" 
     android:inputType="textUri" android:layout_marginRight="45dp" 
     android:layout_height="wrap_content" android:layout_width="fill_parent" /> 
    <Button android:id="@+id/getGraph" android:text="@string/get"  
     android:layout_height="wrap_content" android:width="45dp" 
     android:layout_width="wrap_content" 
     android:layout_weight="1" /> 
</LinearLayout>