设置自定义ActionBar,在左侧和右侧留有空间

阿杰

我想在mainActivity中设置自定义ActionBar。我为名为的操作栏进行了自定义布局custom_header并且宽度在custom_layout中是match_parent,并且在Activity中设置Layoutparams也是MATCH_PARENT。但是它仍然在左侧和右侧留有边距。

这是我的主要活动

主要活动

ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

    ActionBar.LayoutParams params = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.MATCH_PARENT,
            ActionBar.LayoutParams.MATCH_PARENT
    );

    View viewActionBar = ((Activity) MainActivity.this).getLayoutInflater().inflate(R.layout.custom_header,null);
    actionBar.setCustomView(viewActionBar,params);

custom_header.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:padding="10dp"
android:background="@color/md_white_1000"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
    android:src="@drawable/logo"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_width="40dp"
    android:layout_height="40dp" />
<TextView
    android:id="@+id/txtHead"
    android:text="DIGIM"
    android:textColor="@color/colorPrimary"
    android:textSize="20sp"
    android:layout_centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<TextView
    android:text="KEY"
    android:textSize="20sp"
    android:textColor="@color/appdefault"
    android:layout_toRightOf="@+id/txtHead"
    android:layout_centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<ImageButton
    android:src="@drawable/notification_icon"
    android:background="@android:color/transparent"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_centerVertical="true"
    android:layout_width="30dp"
    android:layout_height="30dp" />

   </RelativeLayout>

mainactivity.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<TextView
    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginStart="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:text="@string/title_home"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="0dp"
    android:layout_marginStart="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation" />

   </android.support.constraint.ConstraintLayout>

style.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/appdefault</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>


 </resources>
Anisuzzaman Babla

只需添加此

Toolbar toolbar=(Toolbar)viewActionBar.getParent();
toolbar.setContentInsetsAbsolute(0,0);

在下面提到的行之后

actionBar.setCustomView(viewActionBar,params);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么搜索栏的左侧和右侧都有空间?

Bootstrap 4 自定义标题 css(左侧的徽标,右侧的标题和标题下的图标)

FTRUNCATE在开头留有空间

Cradview 在项目之间留有空间

从片段设置自定义ActionBar标题

在 SwiftUI 中排列自定义视图,没有空间或重叠

如何设置自定义导航栏和自定义折叠?右侧未在移动设备中排列

自定义ActionBar和RoboGuice注入

Qt如何将自定义小部件添加到垂直布局以占用最少的垂直空间(自定义小部件之间没有空间)

Outlook 2019的额外空间。右侧和左侧

图像的空间比页面的左侧和右侧

创建全屏iframe-左侧留有导航空间

自定义软键盘urdu设置光标右侧

TextInputLayout内部的EditText在顶部留有空白空间

FlexSlider自动高度在滑块下方仍留有空间

左右对齐在 StackPanel 中间留有空间

具有代理的自定义元素,获取和设置

具有空值和自定义删除器的shared_ptr怪异

具有空格和路径的自定义bash补全

无法设置ActionBar的自定义背景颜色

将自定义 ActionBar 重力设置为 RIGHT

如何设置自定义ActionBar的颜色/样式?

如何在ActionBar标题中设置自定义字体?

ActionBar设置区域的自定义背景色

WPF:停靠在“顶部”和“底部”的 DockPanel 中的两个控件仍然在底部留有空间

边框左侧和右侧有多余的空间,我该如何解决?

具有搜索视图的Android自定义ActionBar

带有导航下拉列表的自定义ActionBar

使用自定义命名空间和现有模型创建脚手架