ToolBar中的LinearLayout与父级不匹配

时间:2016-01-19 10:49:04

标签: android android-layout

我在工具栏中放了一个LinearLayout1。然后在LinearLayout1中再放了5个LinearLayouts。每个子LinearLayout都有一个ImageView。这里我的问题是LinearLayout1与父宽度不匹配。我在图像中显示(红色圆圈)。黑色是我给ToolBar的背景。

<android.support.v7.widget.Toolbar
    android:id="@+id/featured_bottomToolbar"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="bottom"
    android:background="#000000"
    android:layout_alignParentBottom="true">


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/menu_black_featured"
                android:id="@+id/imageView" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/menu_grey_justin2"
                android:id="@+id/imageView2" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/menu_grey_designers"
                android:id="@+id/imageView3" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/menu_grey_categories"
                android:id="@+id/imageView4" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/meny_grey_more2"
                android:id="@+id/imageView5" />
        </LinearLayout>

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

我得到的输出。

enter image description here

4 个答案:

答案 0 :(得分:55)

将以下属性添加到Toolbar元素。

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

contentInsetStart的默认值为16dp。您需要将其设置为0

答案 1 :(得分:2)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/featured_bottomToolbar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="bottom"
        android:background="#000000"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        android:layout_alignParentBottom="true">


        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="0dp"
                android:layout_weight="2"
                android:layout_height="match_parent">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/menu_black_featured"
                    android:id="@+id/imageView" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="0dp"
                android:layout_weight="2"
                android:layout_height="match_parent">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/menu_grey_justin2"
                    android:id="@+id/imageView2" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="0dp"
                android:layout_weight="2"
                android:layout_height="match_parent">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/menu_grey_designers"
                    android:id="@+id/imageView3" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="0dp"
                android:layout_weight="2"
                android:layout_height="match_parent">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/menu_grey_categories"
                    android:id="@+id/imageView4" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="0dp"
                android:layout_weight="2"
                android:layout_height="match_parent">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/meny_grey_more2"
                    android:id="@+id/imageView5" />
            </LinearLayout>

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

答案 2 :(得分:0)

尝试致电:

mToolbar = (Toolbar) findViewById(R.id.featured_bottomToolbar);
mToolbar.setContentInsetsAbsolute(0, 0);

Toolbar个实例上。

答案 3 :(得分:-1)

您还需要执行以下操作:

getSupportActionBar().setDisplayHomeAsUpEnabled(false);