在我的styles.xml中
<!-- Base application theme. -->
<style name="MainAppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textAllCaps">false</item>
<item name="colorControlNormal">@android:color/white</item>
<item name="colorPrimary">@color/colorPrimary</item>
<!--item name="colorPrimaryDark">@color/colorPrimaryDark</item>-->
<!-- theme UI controls like buttons, checkboxes and text fields, cursor or progress bar-->
<item name="colorAccent">@color/colorAccent</item>
<!-- hide action bar -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
在Manifest.xml中
活动中:
mToolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/tool_bar"/>
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#aabbcc"
android:layout_width="match_parent"
android:layout_height="@dimen/tool_bar_height">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/toolBarConstraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="0dp"
android:layout_height="@dimen/tool_bar_height"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView android:layout_width="32dp"
android:id="@+id/imageView"
android:layout_height="32dp"
android:layout_marginLeft="48dp"
android:layout_marginStart="48dp"
android:src="@drawable/ic_app_logo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="@+id/toolbaTitleTextView"
style="@style/textViewOneLine"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center|start"
android:text="@string/application_name"
app:textAllCaps="true"
android:textColor="@android:color/white"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/toolBar"
app:layout_constraintEnd_toEndOf="@+id/toolBar"
app:layout_constraintTop_toTopOf="@+id/toolBar" app:layout_constraintStart_toEndOf="@+id/imageView"
android:layout_marginStart="8dp" android:layout_marginLeft="8dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>