简体   繁体   English

如何将Button与LinearLayout中的最右角对齐?

[英]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. 这是我用于所有android活动的通用布局代码。 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". 我相信您可以在第二个ImageView和按钮之间添加一个视图,并将其设置为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. 并将android:layout_alignParentRight =“ true”应用于您的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 可以使用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 . 使用RelativeLayout并将其高度设置为所需的最小值 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. 当您使用RelativeLayout时,可以使用android:layout_alignParentRight="true"将任何项目放在右边。 Now, instead of button, you can use image and set its android:scaleType="centerInside" if there are problems with the buttons scaling. 现在,如果按钮缩放存在问题,您可以使用image而不是button并设置其android:scaleType="centerInside" I was having the same problem as you and I sove it using RelativeLayout. 我遇到了与您相同的问题,因此我使用RelativeLayout解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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