本文介绍了在Android工具栏中添加自定义视图时,会有marginLeft的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对android工具栏有一些问题.通常,如果我在工具栏中设置了自定义视图,则该视图应从左到右填充整个工具栏空间,并且没有边距.

I have some problems about android toolbar. Normally if I set a custom view into toolbar, the view should fill the whole toolbar space from left to right and has no margin.

但是我的左边有一个空白,这是我的代码:

but mine has a empty space in the left , these are my code :

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/base_toolbar"
        android:layout_width="match_parent"
        android:layout_height="46dip"
        android:background="?attr/colorPrimary" />

    <FrameLayout
        android:id="@+id/base_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

活动:

private void initToolbar() {
    toolbar = (Toolbar) findViewById(R.id.base_toolbar);
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    if( actionBar != null)
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    contentLayout = (FrameLayout) findViewById(R.id.base_content);
}

这里的代码有什么问题吗?应该是这样吗?

Is here anything wrong with my code or it should be like this ?

推荐答案

左侧的空格是由工具栏的contentInsetStart引起的,默认情况下为16dp.您可以将其设置为0以将其删除.不要忘记添加架构.

The space on the left is caused by Toolbar's contentInsetStart which by default is 16dp. You can set it to 0 to remove it. Dont forget to add the schema.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/base_toolbar"
        android:layout_width="match_parent"
        android:layout_height="46dip"
        android:background="?attr/colorPrimary"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp" />

    <FrameLayout
        android:id="@+id/base_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

这篇关于在Android工具栏中添加自定义视图时,会有marginLeft的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:21