简体   繁体   中英

Android relativeLayout: How to make button take bottom port of screen?

I was wondering can I create a button that would take the bottom portion of the screen as seen here:

在此处输入图片说明

I have tried to play with the alignment and margins, but to no avail:

在此处输入图片说明

Another question that I had was why my movie poster is not aligning to the left of the top text properly? As can be seen, it's a bit to the right. I had attempted to drag it into place.

Here is my XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.abushawish.randommovie.MainActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_alignParentTop="true"
    android:layout_marginTop="14dp"
    android:text="Black Hawk Down"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView3"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:src="@drawable/bhd" />

<Button
    android:id="@+id/button1"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:text="New Movie Suggestion"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_marginLeft="-50dp"
    android:layout_marginRight="-50dip"
    android:layout_marginBottom= "-20dp" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_centerVertical="true"
    android:text="Description"
    android:layout_marginLeft="1dp" />

Thank you!

You should start by removing the padding and margins in order to have a simple layout. When it is ok, you can put back the padding & margins one by one to see which ones are ok or not.

Remove the padding from the buttons parent layout, then your button will align with the edges of the screen.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.abushawish.randommovie.MainActivity$PlaceholderFragment" >

If you still want to have padding for the other objects add them in a separate layout with padding.

Edit

To put the picture in the middle add

android:layout_centerHorizontal="true"

尝试android:gravity =“ bottom”并在相对布局中添加android:layout_alignParentBottom =“ true”

Try to remove these.

android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"

They add padding to the RelativeLayout.

This will give u the intended look.. and yes, don't give weight to children of RelativeLayout

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="New Movie Suggestion" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM