简体   繁体   English

Android:从操作栏的自定义布局中删除左边距

[英]Android: remove left margin from actionbar's custom layout

I am using a custom actionbar view, and as you can see in the screenshot below, there is a blank gray space in the actionbar.我正在使用自定义操作栏视图,正如您在下面的屏幕截图中看到的那样,操作栏中有一个空白的灰色空间。 I want to remove it.我想删除它。

在此处输入图像描述

What have I done:我做了什么:

res/values-v11/styles.xml res/values-v11/styles.xml

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">
        <item name="android:actionBarStyle">@style/ActionBarStyle</item>
        <item name="actionBarStyle">@style/ActionBarStyle</item>
</style>

res/values/my_custom_actionbar.xml res/values/my_custom_actionbar.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="ActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
        <item name="android:height">60dp</item>
    </style>
</resources>

Manifest显现

<uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="19" />

<application
            android:icon="@drawable/ic_launcher"
            android:label="@string/AppName"
            android:theme="@style/AppBaseTheme" >
    <!-- activities... etc -->
</application>

MainActivity主要活动

public void onCreate(Bundle bundle) {
    super.onCreate(bundle);

    ActionBar actionbar = getSupportActionBar();

    actionbar.setDefaultDisplayHomeAsUpEnabled(false);
    actionbar.setDisplayHomeAsUpEnabled(false);
    actionbar.setDisplayShowCustomEnabled(true);
    actionbar.setDisplayShowHomeEnabled(false);
    actionbar.setDisplayShowTitleEnabled(false);
    actionbar.setDisplayUseLogoEnabled(false);
    actionbar.setHomeButtonEnabled(false);

    // Add the custom layout
    View view = LayoutInflater.from(this).inflate(R.layout.actionbar, null, false);
    actionbar.setCustomView(view);
}

I have found a recent post, that is pointing out that there is an issue with the latest release.我发现了最近的一篇文章,指出最新版本存在问题。 I have also updated ADT and SDK to Android 5.我还将 ADT 和 SDK 更新为 Android 5。

Android ActionBar's custom view not filling parent Android ActionBar 的自定义视图未填充父级

I don't know what should I do.我不知道我该怎么办。

Edit (partial solution):编辑(部分解决方案):

Not working on Android <= API 10.不适用于 Android <= API 10。

Android Lollipop, AppCompat ActionBar custom view doesn't take up whole screen width Android Lollipop,AppCompat ActionBar 自定义视图不占用整个屏幕宽度

What have I changed:我改变了什么:

Use the latest sdk version:使用最新的 sdk 版本:

<uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="21" />

Add a toolbarStyle :添加toolbarStyle

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">
        <item name="android:actionBarStyle">@style/ActionBarStyle</item>
        <item name="actionBarStyle">@style/ActionBarStyle</item>

        <item name="android:toolbarStyle">@style/ToolbarStyle</item>
        <item name="toolbarStyle">@style/ToolbarStyle</item>
</style>

<style name="ToolbarStyle" parent="@style/Widget.AppCompat.Toolbar">
    <item name="contentInsetStart">0dp</item>
    <item name="android:contentInsetStart">0dp</item>
</style>

If you are adding the Toolbar via XML, you can simply add XML attributes to remove content insets.如果您通过 XML 添加Toolbar ,您可以简单地添加 XML 属性来删除内容插入。

<android.support.v7.widget.Toolbar
    xmlns:app="schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/primaryColor"
    android:contentInsetLeft="0dp"
    android:contentInsetStart="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    android:contentInsetRight="0dp"
    android:contentInsetEnd="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetEnd="0dp" />

try this:尝试这个:

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);
    View customView = getLayoutInflater().inflate(R.layout.main_action_bar, null);
    actionBar.setCustomView(customView);
    Toolbar parent =(Toolbar) customView.getParent();
    parent.setPadding(0,0,0,0);//for tab otherwise give space in tab
    parent.setContentInsetsAbsolute(0,0);

I used this code in my project,good luck;我在我的项目中使用了这段代码,祝你好运;

The left inset is caused by Toolbar's contentInsetStart which by default is 16dp.左插图是由 Toolbar 的contentInsetStart引起的,默认为 16dp。

Change this to align to the keyline.更改它以与关键线对齐。

Update for support library v24.0.0:支持库 v24.0.0 的更新:

To match the Material Design spec there's an additional attribute contentInsetStartWithNavigation which by default is 16dp.为了匹配 Material Design 规范,还有一个额外的属性contentInsetStartWithNavigation ,默认为 16dp。 Change this if you also have a navigation icon.如果您还有导航图标,请更改此设置。

It turned out that this is part of a new Material Design Specification introduced in version 24 of Design library.事实证明,这是设计库第 24 版中引入的新材料设计规范的一部分。

https://material.google.com/patterns/navigation.html https://material.google.com/patterns/navigation.html

However, it is possible to remove the extra space by adding the following property to Toolbar widget.但是,可以通过将以下属性添加到工具栏小部件来删除额外的空间。

app:contentInsetStartWithNavigation="0dp"

Before :前 : 在此处输入图像描述

After :后 : 在此处输入图像描述

I found an other resolution (reference appcompat-v7 ) that change the toolbarStyle ,following code:我发现了另一个改变 toolbarStyle 的解决方案(参考 appcompat-v7 ),代码如下:

<item name="toolbarStyle">@style/Widget.Toolbar</item>


<style name="Widget.Toolbar" parent="@style/Widget.AppCompat.Toolbar">
<item name="contentInsetStart">0dp</item>
</style>
<android.support.v7.widget.Toolbar
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="wrap_content"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:paddingLeft="0dp">

This should be good enough.这应该足够好了。

只需将app:contentInsetStart="0dp"添加到工具栏的 XML 属性即可。

Just Modify your styles.xml只需修改您的styles.xml

<!-- ActionBar styles -->
    <style name="MyActionBar" parent="Widget.AppCompat.ActionBar">
        <item name="contentInsetStart">0dp</item>
        <item name="contentInsetEnd">0dp</item>
    </style>

Only add app:contentInsetStart="0dp" to the toolbar remove that left space.仅将app:contentInsetStart="0dp"添加到工具栏删除剩余的空间。

So your Toolbar definition look like this所以你的 Toolbar 定义看起来像这样

 <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    app:contentInsetStart="0dp"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

and it look like this它看起来像这样

在此处输入图像描述

Instead of adding a toolbar in the layout, you can set your custom view as shown below.您可以设置自定义视图,而不是在布局中添加工具栏,如下所示。

Toolbar parent = (Toolbar) customView.getParent();
parent.setContentInsetsAbsolute(0,0);

If you check the documentation of toolbar, by default it has style defined and there is an item contentInsetStart.如果您查看工具栏的文档,默认情况下它已定义样式并且有一个项目 contentInsetStart。

<item name="contentInsetStart">16dp</item>

If you want to override this padding then it can be done in two ways.如果您想覆盖此填充,则可以通过两种方式完成。

1.Override the style and add your own style with contentInsetStart and contentInsetEnd values 1.覆盖样式并使用 contentInsetStart 和 contentInsetEnd 值添加您自己的样式

<style name="MyActionBar" parent="Widget.AppCompat.ActionBar">
    <item name="contentInsetStart">0dp</item>
    <item name="contentInsetEnd">0dp</item>
</style>

2.In XML you can directly define the contentInsetStart and contentInsetEnd values 2.在 XML 中可以直接定义 contentInsetStart 和 contentInsetEnd 值

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    android:layout_height="?attr/actionBarSize">

    <!--Add your views here-->

</androidx.appcompat.widget.Toolbar>

Using AppCompatAcitivty you can use just by使用AppCompatAcitivty ,您可以通过

Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
View logo = getLayoutInflater().inflate(R.layout.custom_toolbar, null);
mToolbar.addView(logo, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
mToolbar.setContentInsetsAbsolute(0,0);

You need to add this line app2:contentInsetStart="0dp" in your toolbar您需要在工具栏中添加这一行 app2:contentInsetStart="0dp"

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app2="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    app2:contentInsetStart="0dp"/>

this work for me这对我有用

toolbar.setPadding(0,0,0,0);
toolbar.setContentInsetsAbsolute(0,0);

Setting "contentInset..." attributes to 0 in the Toolbar didn't work for me.在工具栏中将“contentInset ...”属性设置为 0 对我不起作用。 Nilesh Senta's solution to update the style worked! Nilesh Senta 更新风格的解决方案奏效了!

styles.xml样式.xml

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="actionBarStyle">@style/Actionbar</item>
    <item name="android:titleTextStyle">@style/ActionbarTitle</item>
</style>

<style name="Actionbar" parent="Widget.AppCompat.ActionBar">
    <item name="contentInsetStart">0dp</item>
    <item name="contentInsetEnd">0dp</item>
</style>

java (onCreate) java (onCreate)

ActionBar actionBar =  getSupportActionBar();

actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);

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

View view = LayoutInflater.from(this).inflate(R.layout.actionbar_main, null);

actionBar.setCustomView(view, layoutParams);

Kotlin科特林

    supportActionBar?.displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM;
    supportActionBar?.setCustomView(R.layout.actionbar);

    val parent = supportActionBar?.customView?.parent as Toolbar
    parent?.setPadding(0, 0, 0, 0)//for tab otherwise give space in tab
    parent?.setContentInsetsAbsolute(0, 0)

I did not find a solution for my issue (first picture) anywhere, but at last I end up with a simplest solution after a few hours of digging.我没有在任何地方找到我的问题(第一张图片)的解决方案,但经过几个小时的挖掘,我最终得到了一个最简单的解决方案。 Please note that I tried with a lot of xml attributes like app:setInsetLeft="0dp" , etc.. but none of them helped in this case.请注意,我尝试了很多 xml 属性,例如app:setInsetLeft="0dp"等。但在这种情况下,它们都没有帮助。

Picture 1图片1 在此处输入图像描述

the following code solved this issue as in the Picture 2下面的代码解决了这个问题,如图 2 所示

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

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


    //NOTE THAT: THE PART SOLVED THE PROBLEM.
    android.support.design.widget.AppBarLayout abl = (AppBarLayout)
            findViewById(R.id.app_bar_main_app_bar_layout);

    abl.setPadding(0,0,0,0);
}

Picture 2图二

在此处输入图像描述

The correct and simplest answer is on another post, this: 最正确和最简单的答案在另一篇文章中,这个:

Android Lollipop, AppCompat ActionBar custom view doesn't take up whole screen width Android Lollipop,AppCompat ActionBar自定义视图不占用整个屏幕宽度

It is marked as correct and, after I tried that, I can confirm that it works. 它被标记为正确,在我尝试之后,我可以确认它是有效的。

Create toolbar like this:像这样创建工具栏:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/menuToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:background="@color/white"
android:contentInsetLeft="10dp"
android:contentInsetRight="10dp"
android:contentInsetStart="10dp"
android:minHeight="?attr/actionBarSize"
android:padding="0dp"
app:contentInsetLeft="10dp"
app:contentInsetRight="10dp"
app:contentInsetStart="10dp"></android.support.v7.widget.Toolbar>

please follow this link for more - Android Tips请点击此链接了解更多信息 - Android 提示

only adding android:padding="0dp" work for me只添加android:padding="0dp"对我有用

<android.support.v7.widget.Toolbar
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:padding="0dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
ActionBar ab = getSupportActionBar();
ab.setDisplayShowHomeEnabled(true);
      ab.setDisplayShowCustomEnabled(true);
      setDisplayShowTitleEnabled(true);
      View customView =     getLayoutInflater().inflate(R.layout.activity_main,null); //here activity_main.xml is the GUI design file.
ab.setCustomView(customView);
Toolbar parent =(Toolbar) customView.getParent(); //use V7 Toolbar import
parent.setContentInsetsAbsolute(0, 0);
setPadding(5,0,0,0);

ab.setIcon(R.mipmap.ic_launcher);

You can just use relative layout inside toolbar view group in your xml file and adjust the positions of widgets as you require them for your use case.No need to create custom layout & inflate it and attach to toolbar.您可以在 xml 文件中的工具栏视图组内使用相对布局,并根据用例的需要调整小部件的位置。无需创建自定义布局并将其膨胀并附加到工具栏。 Once done in your java code use setContentInsetsAbsolute(0,0) with your toolbar object before setting it as support action bar in your layout.在您的 java 代码中完成后,将 setContentInsetsAbsolute(0,0) 与您的工具栏对象一起使用,然后再将其设置为布局中的支持操作栏。

It would be better to add a background item into the style of the app actionbar to consistent with the background color of the customized actionbar:最好在 app actionbar 的样式中添加一个背景项,使其与自定义 actionbar 的背景颜色保持一致:

<item name="android:background">@color/actionbar_bgcolor</item>

After android 6.0, the actionbar even has a margin-right space and cannot be set. android 6.0之后,actionbar甚至还有margin-right空间,不能设置。 Add a right margin adjusting in the activity like this: (the view is the customized actionbar or a right button in it)像这样在活动中添加右边距调整:(视图是自定义的操作栏或其中的右键)

int rightMargin = Build.VERSION.SDK_INT>=Build.VERSION_CODES.M ? 0 : 8; // may the same with actionbar leftMargin in px
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
p.setMargins(p.leftMargin, p.topMargin, rightMargin, p.bottomMargin);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
    p.setMarginEnd(rightMargin);
}
view.setLayoutParams(p);

The actionbar machenism is just supported after Android 3.0+ app.仅在 Android 3.0+ 应用程序之后才支持操作栏机制。 If we use a toolbar (of support lib v7) instead, we should layout it in each xml of each activity, and take care of the issue of overlapping with system status bar in the Android 5.0 or later device.如果我们使用工具栏(支持 lib v7)代替,我们应该在每个活动的每个 xml 中布局它,并注意在 Android 5.0 或更高版本的设备中与系统状态栏重叠的问题。

In xml add these two attribute for Toolbar tag:在 xml 中为 Toolbar 标签添加这两个属性:

app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"

and inside code add these lines:并在内部代码添加以下行:

// remove extra padding of toolbar
toolbar.setContentInsetsAbsolute(0, 0);
toolbar.setPadding(0, 0, 0, 0);

Tested on mobile and tablet devices both.在移动和平板设备上都进行了测试。

<androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="68dp"
            android:elevation="12dp"
            app:contentInsetStart="0dp">

Does your app support android 5 ? 你的应用支持android 5吗? If not support then you can downgrade version of support actionbar v7. 如果不支持,那么您可以降级支持操作栏v7的版本。 It's look like: 它看起来像:

compile 'com.android.support:appcompat-v7:19.0.+' (Don't use v7:21+) 编译'com.android.support:appcompat-v7:19.0.+'(不要使用v7:21+)

also make target version of application less than 21. 也使目标版本的应用程序少于21。

targetSdkVersion 14 targetSdkVersion 14

If your app support android 5 -> you will need custom ToolBar. 如果您的应用支持android 5 - >,您将需要自定义ToolBar。 (Becuase appcombat-v7:21 have some change). (因为appcombat-v7:21有一些变化)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM