[英]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.