【问题标题】:Is it possible to seperate elements in actionbar?是否可以在操作栏中分隔元素?
【发布时间】:2022-01-02 20:33:55
【问题描述】:

是否可以像图片中那样在操作栏中分离元素以及如何做到这一点? 右侧的按钮来自操作栏的菜单,其中 app:showAsAction="always" 我只需要像这样制作动作栏:

【问题讨论】:

  • Toolbar 只是一个 ViewGroup 。您可以将视图放入其中,使其看起来像这样。
  • @adm 你能给我任何提示或代码示例吗,请

标签: android xml kotlin android-actionbar toolbar


【解决方案1】:

是的,您可以轻松做到这一点

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">


        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/my_toolbar"
            android:layout_width="0dp"
            android:layout_height="?actionBarSize"
            android:background="@drawable/toolbar_bg"
            app:titleTextColor="@color/white"
            android:layout_weight="1"/>


        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_android_black_24dp"
            android:layout_gravity="center_vertical"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="20dp" />
        
    </LinearLayout>
    
    //other views


</LinearLayout>

toolbar_bg.xml

<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/purple_500"/>
    <corners android:topRightRadius="20dp" android:bottomRightRadius="20dp"/>
</shape>

MainActivity.java

Toolbar toolbar = findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);

还将应用程序的主题从themes.xml更改为无操作栏

输出

【讨论】:

  • 谢谢你的回答,我的错我没有提到那个按钮来自actionbar的菜单,其中app:showAsAction="always"
  • 你能做同样的事情,但是对于操作栏菜单项,请
  • @Diset 我不认为可以,但是您可以放置​​任何按钮并使用上下文菜单来显示菜单
猜你喜欢
  • 2011-01-09
  • 2010-10-16
  • 2012-04-23
  • 1970-01-01
  • 1970-01-01
  • 2012-10-12
  • 2018-12-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多