【发布时间】:2022-01-02 20:33:55
【问题描述】:
【问题讨论】:
-
Toolbar 只是一个 ViewGroup 。您可以将视图放入其中,使其看起来像这样。
-
@adm 你能给我任何提示或代码示例吗,请
标签: android xml kotlin android-actionbar toolbar
【问题讨论】:
标签: android xml kotlin android-actionbar toolbar
是的,您可以轻松做到这一点
<?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更改为无操作栏
输出
【讨论】: