ToolBar中的LinearLayout未能匹配父容器大小。

17

我在工具栏中放置了一个LinearLayout1。然后在LinearLayout1中放置了另外5个LinearLayout。每个子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>

我得到的输出。

在此输入图像描述


1
请发布您的代码,以便我们能够帮助您。 - Suhas Bachewar
您是在使用工具栏手动创建TabHost吗? - Tim
我已经发布了。请阅读并帮忙。 - Thomas M Kurialassery
4个回答

71

在您的 工具栏 元素中添加以下属性。

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

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


3
<?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>

我已经添加了以下内容: //在外部布局中 xmlns:app="http://schemas.android.com/apk/res-auto"//在工具栏中 app:contentInsetLeft="0dp" app:contentInsetStart="0dp" - Suhas Bachewar

0

尝试调用:

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

在您的工具栏实例上。


-1

你还需要做:

getSupportActionBar().setDisplayHomeAsUpEnabled(false);

如果您使用自己的工具栏,它就会无用并崩溃。 - SoftDesigner

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接