简体   繁体   English

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

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

I am trying to add into the toolbar a TextView and a Spinner to implement language change from there.我正在尝试在工具栏中添加一个TextView和一个Spinner以从那里实现语言更改。 The problem it's that I added a LinearLayout to the toolbar but I cannot manage to move it to the right side so it won't get over the title of the activity, or to stay next to it.问题是我在toolbar添加了一个LinearLayout但我无法将它移动到右侧,因此它不会越过活动的标题,或者留在它旁边。

Right now the LinearLayout is set like that (+ where i want it to be):现在LinearLayout是这样设置的(+我想要的位置):

在此处输入图片说明

.xml code: .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> 

And the result it is :结果是:

在此处输入图片说明

I am trying to achieve something like :我正在尝试实现以下目标:

在此处输入图片说明

I achieved that by setting a android:paddingLeft but that wont work on all the devices the same way, so I am looking for another solution.我通过设置android:paddingLeft实现了这一点,但这不会以相同的方式在所有设备上工作,所以我正在寻找另一种解决方案。

I have tried adding gravity but it wasn't moving my layout.我曾尝试添加gravity但它并没有移动我的布局。

add android:layout_gravity="right" in Linear layout.在线性布局中添加android:layout_gravity="right"

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

Wrap the LinearLayout with a RelativeLayout and provide用 RelativeLayout 包装 LinearLayout 并提供

android:layout_alignParentRight="true" android:layout_alignParentRight="true"

property to LinearLayout.属性为 LinearLayout。

Try this,尝试这个,

<?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