Android RelativeLayout with wrap_content truncates

2019-08-28 00:24发布

问题:

I have a RelativeLayout whose width/height is set to wrap_content. However, the button in the bottom right is truncated on its lower side. Changing the RelativeLayout's paddingBottom doesn't help, neither does adding a marginBottom to the button.

Any ideas?

Here's my code:

<RelativeLayout
    android:id="@+id/sign_up_phase_enteraddress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:paddingTop="10dp"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    android:paddingLeft="10dp"
    android:background="#ff6aa639"
     >

    <TextView
        android:id="@+id/sign_up_signuphere"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="29dp"
        android:text="@string/sign_up_signuphere"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/sign_up_emaillabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/sign_up_signuphere"
        android:layout_alignLeft="@id/sign_up_signuphere"
        android:layout_alignBaseline="@+id/sign_up_email"
        android:text="@string/sign_up_emailaddress"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/sign_up_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="46dp"
        android:layout_below="@id/sign_up_signuphere"
        android:layout_toRightOf="@id/sign_up_emaillabel"
        android:ems="10"
        android:inputType="textEmailAddress" >

        <requestFocus />

    </EditText>

    <Button
        android:id="@+id/sign_up_signup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/sign_up_haveaccount"
        android:layout_alignRight="@id/sign_up_email"
        android:layout_below="@id/sign_up_email"
        android:text="@string/sign_up_signup" />

    <TextView
        android:id="@+id/sign_up_haveaccount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/sign_up_emaillabel"
        android:layout_marginTop="27dp"
        android:layout_below="@id/sign_up_email"
        android:text="@string/sign_up_haveaccount"
        android:textAppearance="?android:attr/textAppearanceSmall" />


</RelativeLayout>

...and this is what it looks like in the emulator:

Any thoughts on how I can get the bottom of my button to show? TIA!

回答1:

My fast idea is to set button bottom border to height of text on the left by using xml code

android:layout_alignBottom="@+id/sign_up_haveaccount"

because you've setted layout_below option already. However your xml code is quite not proper :) I will try write here better xml code for you, later.


Well, I found your problem. And I was searching for solution. I setted for textview new padding bottom because button was aligned to this textview basline. I know that it could be a nice hack but I coudln't find andy good way to handle it :)

According to your layout I changed behaviour for edittext to make it stretchable. Also I grouped all elements into 3 groups. Each one per row to make it more readable.

Also do not forget that first 2 lines about xmls.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/sign_up_phase_enteraddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#ff6aa639"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >

<!-- 1 row -->

<RelativeLayout
    android:id="@+id/row1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="29dp" >

    <TextView
        android:id="@+id/sign_up_signuphere"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sign_up_signuphere"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>

<!-- 2 row -->

<RelativeLayout
    android:id="@+id/row2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/row1" >

    <TextView
        android:id="@+id/sign_up_emaillabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/sign_up_email"
        android:text="@string/sign_up_emailaddress"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/sign_up_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="20dp"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/sign_up_emaillabel"
        android:inputType="textEmailAddress" >

        <requestFocus />
    </EditText>
</RelativeLayout>

<!-- 3 row -->

<RelativeLayout
    android:id="@+id/row3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/row2" >

    <TextView
        android:id="@+id/sign_up_haveaccount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="27dp"
        android:paddingBottom="15dp"
        android:text="@string/sign_up_haveaccount"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <Button
        android:id="@+id/sign_up_signup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/sign_up_haveaccount"
        android:layout_alignParentRight="true"
        android:text="@string/sign_up_signup" />
</RelativeLayout>

</RelativeLayout>


回答2:

This is happening because of your padding , you have given 10 dp padding in bottom and your button view doesn't have enough space.Remove padding and check it .