android - Android Toolbar添加自定义 View 时,会有一个marginLeft

标签 android toolbar android-custom-view

我有一些关于安卓工具栏的问题。通常,如果我将自定义 View 设置到工具栏中,该 View 应该从左到右填满整个工具栏空间并且没有边距。

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

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>

Activity :

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);
}

我的代码有什么问题还是应该是这样的?

最佳答案

左边的空白是由Toolbar的contentInsetStart造成的,默认是16dp。您可以将其设置为 0 以将其删除。不要忘记添加架构。

<?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 - Android Toolbar添加自定义 View 时,会有一个marginLeft,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32086148/

相关文章:

Android 小部件如何从中获取小时和分钟的时间?

java - Android Studio 不正确关机后损坏的 .java 文件

java - setMargins 方法不适用于自定义 ViewGroup

android - 适用于 Android 平台的音频/视频编解码器

android - 只能在 facebook 请求前向 Bundle 添加一个参数

c# - 在 Xamarin.Forms 中更改工具栏图标

javascript - 从工具栏的 JavaScript 中显示 FireFox 工具栏

delphi - 如何让快捷键根据事件选项卡页执行不同的操作?

android定义的属性和自定义属性并排在自定义 View 上

android - Android 如何处理 subview 中触摸点的转换?