简体   繁体   中英

How to align a Button to the most right corner in LinearLayout?

Please have a look at the following code

<?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="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#373734"
    android:orientation="horizontal" >

        <ImageView
            android:id="@+id/backToLanguageSelectionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="15dp"
            android:paddingTop="5dp"
            android:src="@drawable/thunderbolt" />

        <ImageView
            android:id="@+id/internetButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="5dp"
            android:paddingTop="5dp"
            android:src="@drawable/globe_small_2" />

        <Button
            android:id="@+id/giveUpButton"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="350dp"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:text="Button" />



</LinearLayout>

This is a common layout code which I am using for all the android activities. The problem is the button, I want it to move to the most right corner. Margin thing not seems to work properly because in different devices the button is in different places.

How can I move this to the most right corner which will be displayed same in all the devices?

I believe you could add a view between your second ImageView and your button, and set it to layout_width="0dp", layout_weight="1". And remove the left margin of your button.

User Relative layout instead. And apply android:layout_alignParentRight="true" to your Button.

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#373734"
android:orientation="horizontal" >

    <ImageView
        android:id="@+id/backToLanguageSelectionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="15dp"
        android:paddingTop="5dp"
        android:src="@drawable/social_share" />

    <ImageView
        android:id="@+id/internetButton"
        android:layout_toRightOf="@+id/backToLanguageSelectionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"
        android:src="@drawable/social_share" />

    <Button
        android:id="@+id/giveUpButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:text="Button" />

您是否尝试过android:layout_gravity="right"

It can be done with RelativeLayout

<Button
    android:id="@+id/giveUpButton"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginLeft="350dp"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:text="Button"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true" />

Use RelativeLayout and set its height to be as small as you want . This way a small part of your screen will be filled. When you use RelativeLayout, you can use android:layout_alignParentRight="true" to put any item at right. Now, instead of button, you can use image and set its android:scaleType="centerInside" if there are problems with the buttons scaling. I was having the same problem as you and I sove it using RelativeLayout.

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