简体   繁体   中英

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

Right now the LinearLayout is set like that (+ where i want it to be):

在此处输入图片说明

.xml code:

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

I have tried adding gravity but it wasn't moving my layout.

add android:layout_gravity="right" in Linear layout.

  <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

android:layout_alignParentRight="true"

property to 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>

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