2010-10-15 242 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>