2015-09-01 116 views
0

有一个非常类似的问题在这里:如何将edittext和按钮在relativelayout内相互对齐?

How to put an EditText and Button next to each other?

然而,这一问题有一个的LinearLayout作为根元素,我的布局不。

我也已经看过这篇文章:

http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html

这是做实际上我希望做同样的事情,但我的设备一直显示在EditText元素的顶部按钮

Button on top of EditText

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent"> 

    <EditText 
     android:id="@+id/editText" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:singleLine="true" 
     android:inputType="text" /> 

    <Button 
     android:id="@+id/search_button" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignEnd="@+id/editText" 
     android:layout_alignRight="@+id/editText" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:text="@string/search_button" /> 
</RelativeLayout> 

理想情况下,EditText元素应该是我的设备宽度的75%左右,按钮剩余的25%我有使用布局的接近0经验,这是我第一次尝试从一个非常简单的应用程序来自网页背景的划痕。

+0

我从来没有tryit但可相对百分布局新程序兼容性API时,您可以定义宽度和高度百分比尝试。 https://developer.android.com/tools/support-library/features.html#percent。 或使用线性布局 – mcd

+1

另一个例子,你可以在这里找到:http://www.mkyong.com/android/android-relativelayout-example/ –

回答

1

您应该改用LinearLayout。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent"> 

    <EditText 
     android:id="@+id/editText" 
     android:layout_width="0dp" 
     android:weight="1" 
     android:layout_height="wrap_content" 
     android:singleLine="true" 
     android:inputType="text" /> 

    <Button 
     android:id="@+id/search_button" 
     android:layout_width="0dp" 
     android:weight="3" 
     android:layout_height="wrap_content"   
     android:text="@string/search_button" /> 
</LinearLayout > 

编辑: 替代解决方案,但这种方式EDITTEXT是固定的宽度和按键让所有的剩余宽度

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent"> 

    <EditText 
     android:id="@+id/editText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:singleLine="true" 
     android:inputType="text" /> 

    <Button 
     android:id="@+id/search_button" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/editText" 
     android:layout_toEndOf="@+id/editText" 
     android:text="@string/search_button" /> 
</RelativeLayout> 
+0

这可能会进入不成熟的优化,但Android开发人员职位使案件反对使用' LinearLayout',它们只是使用RelativeLayout来做类似的例子。 是否有任何技术功能阻止我使用'RelativeLayout'而不是'LinearLayout'? – ILikeTacos

+0

检查相对布局替代 –

+1

实际上第二种解决方案更好,但是如果您坚持需要**百分比**宽度,则可能需要LinearLayout,或者您可以尝试用PercentageRelativeLayout替换第二个解决方案。但是,不要太信任性能影响。绩效影响的论点是在几年前开始的,那时候这仍然不是问题。你应该忘记它,除非你有超过3或4层的LinearLayout。 –

0

使用属性android:layout_toRightOf = "@id/editText"按钮,并给予固定宽度按钮像这样:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent"> 

    <EditText 
     android:id="@+id/editText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:singleLine="true" 
     android:ems = "15" 
     android:inputType="text" /> 

    <Button 
     android:id="@+id/search_button" 
     android:layout_width="60dp" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/editText" 
     android:text="@string/search_button" /> 
</RelativeLayout> 

不需要我们e额外的属性,如android:layout_alignParentTop

0

首先你给fill_parent作为两个元素的宽度。 fill_parent意味着与父母的宽度相同,因此它们不能水平对齐(顺便说一句,您应该使用match_parent来代替,它们是相同的,但前几年已被弃用...)。让他们坐在彼此旁边,你应该做点像

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

<!-- Put the Button first, so the EditText can occupy all the remaining space --> 
<Button 
    android:id="@+id/search_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:text="@string/search_button" /> 

<EditText 
    android:id="@+id/editText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_toStartOf="@id/search_button" 
    android:layout_toLeftOf="@id/search_button" 
    android:singleLine="true" 
    android:inputType="text" /> 

</RelativeLayout> 

可悲的是你没有办法给一个百分比。要做到这一点,你需要使用LinearLayout并给予权重:

<LinearLayout 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<EditText 
    android:id="@+id/editText" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight=".75" 
    android:singleLine="true" 
    android:inputType="text" /> 

<Button 
    android:id="@+id/search_button" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight=".25" 
    android:text="@string/search_button" /> 

</LinearLayout>