【问题标题】:When add a custom view in Android Toolbar, there will be a marginLeft在Android Toolbar中添加自定义视图时,会有一个marginLeft
【发布时间】:2015-08-19 03:40:59
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: android toolbar android-custom-view


    【解决方案1】:

    左边的空间是由 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>
    

    【讨论】:

    • 这正是我想要的答案。你解决了我的问题真是太好了。非常感谢。
    【解决方案2】:

    你可以这样做,

    <android.support.v7.widget.Toolbar
            android:id="@+id/my_awesome_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#1E90FF"
            android:minHeight="?attr/actionBarSize" >
    
            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="Toolbar Title"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#ffffff" />
        </android.support.v7.widget.Toolbar>
    

    【讨论】:

    • 我试过这个,但没用。无论如何,谢谢你的帮助。
    • 你到底想要什么?是否留有余量?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-03
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 2011-09-06
    • 2021-09-19
    相关资源
    最近更新 更多