Relative layout - stick buttons to bottom

淺唱寂寞╮ 提交于 2019-12-13 06:58:09

问题


I have a relative layout with two sub relative layouts:

The first sub layout contains two text views, the second sub relative layout is below the first sub relative layout. However when I design all looks fine. When rendered on phone the buttons don't align to the second sub relative layouts bottom. Here is my code:

 <RelativeLayout
            android:id="@+id/relative_txt"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignBottom="@+id/relative_img"
            android:layout_alignTop="@+id/relative_img"
            android:layout_marginLeft="05dp"
            android:layout_marginTop="15dp"
            android:layout_toLeftOf="@+id/imageView1"
            android:layout_toRightOf="@+id/relative_img" >

            <RelativeLayout
                android:id="@+id/relative_txt1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentRight="true"
                    android:text="Mike Anderson"
                    android:textColor="#ea2634"
                    android:textSize="14sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/textView2"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/textView2"
                    android:layout_marginTop="02dp"
                    android:text="My New Home"
                    android:textColor="#000000"
                    android:textSize="12sp"
                    android:textStyle="bold" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/relative_btn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_alignLeft="@+id/relative_txt1"
                android:layout_alignRight="@+id/relative_txt1"
                android:layout_alignParentBottom="true"
                android:layout_below="@+id/relative_txt1" >

                <Button
                    android:id="@+id/button1"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentLeft="true"
                    android:layout_marginBottom="04dp"
                    android:background="@drawable/icon_share" />

                <ImageView
                    android:id="@+id/imageView3"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:contentDescription="@string/imageviewContactus"
                    android:src="@drawable/icon_email" />

                <Button
                    android:id="@+id/button2"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:background="@drawable/icon_call" />
            </RelativeLayout>
        </RelativeLayout>

The buttons I want to stick to the bottom shift up and are aligned below the second text view if there is space. Any hints what I am missing?

This is my complete list view row code:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativePrime"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:id="@+id/relative_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/relative_right" >

        <View
            android:id="@+id/leftView"
            android:layout_width="05dp"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/relative_img"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="#560978" />

        <RelativeLayout
            android:id="@+id/relative_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="10dp" >

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:background="@drawable/icon_default"
                android:contentDescription="@string/imageviewContactus" />

            <TextView
                android:id="@+id/dateText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/imageView2"
                android:layout_centerHorizontal="true"
                android:text="TextView" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/relative_txt"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignBottom="@+id/relative_img"
            android:layout_alignTop="@+id/relative_img"
            android:layout_marginLeft="05dp"
            android:layout_marginTop="15dp"
            android:layout_toLeftOf="@+id/imageView1"
            android:layout_toRightOf="@+id/relative_img" >

            <RelativeLayout
                android:id="@+id/relative_txt1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentRight="true"
                    android:text="Mike Anderson"
                    android:textColor="#ea2634"
                    android:textSize="14sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/textView2"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/textView2"
                    android:layout_marginTop="02dp"
                    android:text="My New Home"
                    android:textColor="#000000"
                    android:textSize="12sp"
                    android:textStyle="bold" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/relative_btn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_alignLeft="@+id/relative_txt1"
                android:layout_alignRight="@+id/relative_txt1"
                android:layout_alignParentBottom="true"
                android:layout_below="@+id/relative_txt1" >

                <Button
                    android:id="@+id/button1"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentLeft="true"
                    android:layout_marginBottom="04dp"
                    android:background="@drawable/icon_share" />

                <ImageView
                    android:id="@+id/imageView3"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:contentDescription="@string/imageviewContactus"
                    android:src="@drawable/icon_email" />

                <Button
                    android:id="@+id/button2"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:background="@drawable/icon_call" />
            </RelativeLayout>
        </RelativeLayout>

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/icon_pet_2x" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relative_right"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/relative_left"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >

    </RelativeLayout>

</RelativeLayout>

Desired:

Current status:


回答1:


try this.. if it works for you change height according to your requirement.

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativePrime"
    android:layout_width="fill_parent"
    android:layout_height="100dp" >

    <RelativeLayout
        android:id="@+id/relative_left"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_toLeftOf="@+id/relative_right" >

        <View
            android:id="@+id/leftView"
            android:layout_width="05dp"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/relative_img"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="#560978" />

        <RelativeLayout
            android:id="@+id/relative_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="10dp" >

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:background="@drawable/ic_launcher"
                android:contentDescription="@string/app_name" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/relative_txt"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignBottom="@+id/relative_img"
            android:layout_alignTop="@+id/relative_img"
            android:layout_marginLeft="05dp"
            android:layout_marginTop="15dp"
            android:layout_toLeftOf="@+id/imageView1"
            android:layout_toRightOf="@+id/relative_img" >

            <RelativeLayout
                android:id="@+id/relative_txt1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentRight="true"
                    android:text="Mike Anderson"
                    android:textColor="#ea2634"
                    android:textSize="14sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/textView2"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/textView2"
                    android:layout_marginTop="02dp"
                    android:text="My New Home"
                    android:textColor="#000000"
                    android:textSize="12sp"
                    android:textStyle="bold" />
            </RelativeLayout>
        </RelativeLayout>

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_launcher" />

        <RelativeLayout
            android:id="@+id/relative_btn"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignTop="@+id/dateText"
            android:layout_toLeftOf="@+id/imageView1"
            android:layout_toRightOf="@+id/dateText" >

            <Button
                android:id="@+id/button1"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_marginBottom="04dp"
                android:background="@drawable/ic_launcher" />

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:contentDescription="@string/app_name"
                android:src="@drawable/ic_launcher" />

            <Button
                android:id="@+id/button2"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:background="@drawable/ic_launcher" />
        </RelativeLayout>

        <TextView
            android:id="@+id/dateText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/relative_img"
            android:layout_alignParentBottom="true"
            android:text="TextView" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relative_right"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/relative_left"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >

    </RelativeLayout>

</RelativeLayout> 



回答2:


You can make this layout using LinearLayout and FrameLayout. You can try this layout

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

    <View
        android:id="@+id/leftView"
        android:layout_width="5dp"
        android:layout_height="match_parent"
        android:background="#560978" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:paddingLeft="6dp"
        android:paddingRight="6dp"
        android:paddingTop="8dp" >

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/imageviewContactus"
            android:src="@drawable/icon_default" />

        <TextView
            android:id="@+id/dateText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:singleLine="true"
            android:text="Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:paddingLeft="12dp"
        android:paddingTop="8dp" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mike Anderson"
            android:textColor="#ea2634"
            android:textSize="14sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="02dp"
            android:text="My New Home"
            android:textColor="#000000"
            android:textSize="12sp"
            android:textStyle="bold" />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:paddingBottom="16dp"
            android:paddingTop="16dp" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="right|center_vertical"
                android:contentDescription="@string/imageviewContactus"
                android:src="@drawable/icon_pet_2x" />
        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="4dp"
            android:paddingTop="4dp" >

            <Button
                android:id="@+id/button1"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_gravity="left|center_vertical"
                android:background="@drawable/icon_share" />

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_gravity="center"
                android:contentDescription="@string/imageviewContactus"
                android:src="@drawable/icon_email" />

            <Button
                android:id="@+id/button2"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_gravity="right|center_vertical"
                android:background="@drawable/icon_call" />
        </FrameLayout>
    </LinearLayout>
    <!-- for right side's red portion -->

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/imageviewContactus"
            android:src="@drawable/ic_launcher" />
    </FrameLayout>

</LinearLayout>



回答3:


<RelativeLayout
            android:id="@+id/relative_txt"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="05dp"
            android:layout_marginTop="15dp" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="Mike Anderson"
                android:textColor="#ea2634"
                android:textSize="14sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignRight="@+id/textView2"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/textView2"
                android:layout_marginTop="02dp"
                android:text="My New Home"
                android:textColor="#000000"
                android:textSize="12sp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:contentDescription="@string/imageviewContactus"
                android:src="@drawable/icon_email" />

            <Button
                android:id="@+id/button2"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:background="@drawable/icon_call" />

            <Button
                android:id="@+id/button1"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:background="@drawable/icon_share" />

        </RelativeLayout>

I have made some changes:

1- In the parent tag of relative layout i have removed android:layout_alignBottom, android:layout_alignTop, android:layout_toLeftOf, and android:layout_toRightOf no need for them because you already set width and height match_parent.

2- In textView2 tag removed android:layout_alignParentRight because using it with android:layout_alignParentLeft can be replaced by setting width with match_parent

3- In textView3 changed android:layout_alignLeft to textView2 with android:layout_alignRight because doesn't make sense to align left for a view and align right to parent !!.

4- In button1 tag removed android:layout_alignTop tag because it refer to a view which is not in the same scope "the parent relative layout" so this line will not work.

Please make those changes and make screen shot for the result.



来源:https://stackoverflow.com/questions/26582524/relative-layout-stick-buttons-to-bottom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!