繁体   English   中英

如何将 LinearLayout 移动到工具栏的右侧

[英]How to move a LinearLayout to the right side of the toolbar

我正在尝试在工具栏中添加一个TextView和一个Spinner以从那里实现语言更改。 问题是我在toolbar添加了一个LinearLayout但我无法将它移动到右侧,因此它不会越过活动的标题,或者留在它旁边。

现在LinearLayout是这样设置的(+我想要的位置):

在此处输入图片说明

.xml 代码:

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/limba"
        android:textColor="@color/white"
        android:textSize="15sp"
        android:layout_gravity="center"/>
    <Spinner
        android:layout_marginLeft="5dp"
        android:id="@+id/spinner_limba"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </LinearLayout>
    </androidx.appcompat.widget.Toolbar>

</com.google.android.material.appbar.AppBarLayout> 

结果是:

在此处输入图片说明

我正在尝试实现以下目标:

在此处输入图片说明

我通过设置android:paddingLeft实现了这一点,但这不会以相同的方式在所有设备上工作,所以我正在寻找另一种解决方案。

我曾尝试添加gravity但它并没有移动我的布局。

在线性布局中添加android:layout_gravity="right"

  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right">
 </LinearLayout>

用 RelativeLayout 包装 LinearLayout 并提供

android:layout_alignParentRight="true"

属性为 LinearLayout。

尝试这个,

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        >
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Test"
                    android:textSize="15sp"
                    android:layout_gravity="center"/>
                <Spinner
                    android:layout_marginLeft="5dp"
                    android:id="@+id/spinner_limba"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </RelativeLayout>
    </androidx.appcompat.widget.Toolbar>

</com.google.android.material.appbar.AppBarLayout>

暂无
暂无

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

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