问题描述:

Hi all I have to add 2 Buttons below editfield. I had tried it using relativelayout. But it is not added exactly below mention position.

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

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:fillViewport="true" >

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#E8E8E8" >

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

android:padding="10dip" >

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="5dp"

android:orientation="horizontal" >

<TextView

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="0.35"

android:text="User Name"

android:textStyle="bold" />

<EditText

android:id="@+id/name"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="0.55" />

</LinearLayout>

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="5dp"

android:orientation="horizontal" >

<TextView

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="0.35"

android:text="Description"

android:textStyle="bold" />

<EditText

android:id="@+id/des"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="0.55" />

</LinearLayout>

<RelativeLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_below="@id/des"

android:padding="10dip">

<Button

android:id="@+id/submit"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:text="Submit" />

<Button

android:id="@+id/cancel"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_toRightOf="@id/submit"

android:text="Cancel" />

</RelativeLayout>

</LinearLayout>

</RelativeLayout>

</ScrollView>

Here I want to add these two buttons exactly below "des" editfield. Thanks in advance

Currently it displays like . But I want these two buttons exactly below last editfield with equal size.

网友答案:

You have just add on your relative layout: layout_gravity on right and width make on wrap_content. like this

<RelativeLayout 
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_below="@id/des" android:layout_gravity="right"
    android:padding="10dip">

    <Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Submit" />

    <Button
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/submit"
        android:text="Cancel" />


</RelativeLayout>

Output looks like:

网友答案:

Ist think you are doing wrong is that : ScrollView never contains Relative Layout as a direct child. it use LinearLayout as a direct child .

use this code:

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dip" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.35"
                android:text="User Name"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/name"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.55" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.35"
                android:text="Description"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/des"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.55" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/des"
            android:orientation="horizontal"
            android:padding="10dip" >

            <Button
                android:id="@+id/submit"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Submit" />

            <Button
                android:id="@+id/cancel"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Cancel" />
        </LinearLayout>

    </LinearLayout>

</ScrollView>
网友答案:

Try this

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

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E8E8E8" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dip" >

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.35"
            android:text="User Name"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.55" />
    </LinearLayout>

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.35"
            android:text="Description"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/des"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.55" />
    </LinearLayout>

<RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/des"
        android:padding="10dip">

        <Button
            android:id="@+id/cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="Cancel" />

        <Button
            android:id="@+id/submit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/cancel"
            android:text="Submit" />

    </RelativeLayout>
</LinearLayout>
</RelativeLayout>

</ScrollView>
网友答案:

Try this: One more thing when you want to do stuff like this, try to see in graphical layout and move elements in that to position you want. Most of time it will work as expected and you can get idea of place too.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#E8E8E8" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dip" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:orientation="horizontal" >

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.35"
                    android:text="User Name"
                    android:textStyle="bold" />

                <EditText
                    android:id="@+id/name"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.55" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:orientation="horizontal" >

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.35"
                    android:text="Description"
                    android:textStyle="bold" />

                <EditText
                    android:id="@+id/des"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.55" />
            </LinearLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/des"
                android:padding="10dip" >

                <Button
                    android:id="@+id/submit"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:text="Submit" />

                <Button
                    android:id="@+id/cancel"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/submit"
                    android:layout_alignBottom="@+id/submit"
                    android:layout_alignParentRight="true"
                    android:text="Cancel" />
            </RelativeLayout>
        </LinearLayout>
    </RelativeLayout>

</ScrollView>
网友答案:

In your case its not possible because you are trying to place a button relative to EditText which is placed inside a LinearLayout, you can place the button relative to linearlayout but not its ChildViews.

Try to bring all the Views inside a single relative layout and place all the Views relative to each other.

网友答案:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E8E8E8" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dip" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.35"
                android:text="User Name"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/name"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.55" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.35"
                android:text="Description"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/des"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.55" />
        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/des"
            android:padding="10dip" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/submit"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="12dp"
                    android:text="Submit" />

                <Button
                    android:id="@+id/cancel"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Cancel" />
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

Hope this will solve your problem

网友答案:

android:layout_below="@id/des" That des is present in LinearLayout, IF you want to give that way, Same RelativeLyout should be given within the layout that des is present and Their parent should be relative layout, to use parementers such as below or above(i.e., with relative to other widget)

相关阅读:
Top