【问题标题】:Android toolbar not sizing correctlyAndroid 工具栏大小不正确
【发布时间】:2021-02-18 22:31:42
【问题描述】:

我有一个 android 工具栏,在工具栏中我有一个 editText,而 editText 正在做我不想要的边距。

这是一张图片:

这里是工具栏代码:

<?xml version="1.0" encoding="utf-8"?>

<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/toolbar_top"
    android:background="@drawable/background_toolbar_top"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/buscador"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="10dp"
        android:textColor="#666"
        android:background="#ccc"
        android:textColorHint="#cccc"
        android:hint="Buscar"/>

</android.support.v7.widget.Toolbar>

工具栏背景:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle">
            <solid android:color="#333"></solid>
        </shape>
    </item>

    <item android:bottom="1dp">
        <shape android:shape="rectangle">
            <solid android:color="#fff"></solid>
        </shape>
    </item>

</layer-list>

【问题讨论】:

    标签: android toolbar


    【解决方案1】:

    问题是为什么Toolbar左边有一个空格。是Toolbar的contentInsetStart,默认是16dp,你可以设置0去掉:

    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    

    希望这会有所帮助:

    <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/toolbar_top"
        android:background="@drawable/background_toolbar_top"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp">
    
        <EditText
            android:id="@+id/buscador"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:textSize="10dp"
            android:textColor="#666"
            android:background="#ccc"
            android:textColorHint="#cccc"
            android:hint="Buscar"/>
    
    </android.support.v7.widget.Toolbar>
    

    您可以在Toolbar 中查看this 以获取有关marginLeft 的更多信息。

    【讨论】:

    • 我的问题是宽度
    • 我在答案中添加了app:contentInsetLeft="0dp"app:contentInsetStart="0dp",请检查。
    • 为了让您的回答对未来的访问者更有用,请说明问题是什么以及您的建议解决问题的方式和原因。
    • @Code-Apprentice 我已经添加了有关答案的更多详细信息,请检查:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    相关资源
    最近更新 更多