自定义操作栏没有占用操作栏的所有空间



在android studio中创建自定义操作栏

自定义操作栏看起来比通常的操作栏小

XML代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#6354B0"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher_round"
android:contentDescription="@string/todo" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/action_bar_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:text="@string/todo"
android:textSize="26sp"
android:textStyle="bold">

</TextView>
<TextView
android:id="@+id/action_bar_subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:gravity="center_horizontal"
android:text="@string/organize_your_day_with_tasks"
android:textSize="16sp">

</TextView>
</LinearLayout>

</LinearLayout>

</RelativeLayout>

操作栏java代码


val actionBar = supportActionBar
actionBar?.displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM
actionBar?.setCustomView(R.layout.todo_action_bar)
actionBar?.setDisplayShowHomeEnabled(true)
actionBar?.setDisplayUseLogoEnabled(true)

建议做点什么

输入图片描述

上面的代码显示了

我在网上尝试了许多解决方案,但没有一个有效。

您需要更新父RelativeLayout

android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"

和TopLinearLayout

android:layout_width="match_parent"
android:layout_height="match_parent"

新的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#6354B0"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher_round"
android:contentDescription="@string/todo" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/action_bar_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:text="@string/todo"
android:textSize="26sp"
android:textStyle="bold">

</TextView>
<TextView
android:id="@+id/action_bar_subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:gravity="center_horizontal"
android:text="@string/organize_your_day_with_tasks"
android:textSize="16sp">

</TextView>
</LinearLayout>

</LinearLayout>

</RelativeLayout>

相关内容

最新更新