如何将下拉列表放在工具栏右侧

7
我的布局如下所示 enter image description here 您可以看到我在工具栏中添加了旋转器,但我想让它右对齐,在工具栏的最右侧显示。
以下是我使用的xml代码。
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical" >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:elevation="4dp"
            android:gravity="right"                          //gravity set to right
            android:background = "@color/color_toolbar"
        >
             <Spinner
                android:id="@+id/spinner_category"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </android.support.v7.widget.Toolbar> 
   </LinearLayout>

spinner没有显示android:layout_gravity 在此输入图片描述 我尝试将重力设置为右侧,但不起作用。我该怎么做?


9
尝试使用 android:layout_gravity="right" 来设置下拉列表。 - Raghunandan
@Raghunandan的spinner在自动建议中没有显示出来。 - karan
不理解你的评论。 - Raghunandan
添加到代码中后就可以运行了,谢谢 @Raghunandan。但是为什么一开始没有显示呢? - karan
让我们在聊天中继续这个讨论 - Raghunandan
显示剩余3条评论
1个回答

4

我遇到了相同的问题。基于问题评论,我已经解决了对齐问题,因此在此保留答案以直接帮助他人。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary">
    <Spinner
        android:id="@+id/toolbar_spinner"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:visibility="visible"
        android:layout_gravity="right"/>
</android.support.v7.widget.Toolbar>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接