相对布局未填充在工具栏xml中

时间:2018-02-21 05:36:33

标签: android xml

我正在使用自定义工具栏创建一个Android应用。每当我使用匹配父项添加相对布局时,它会显示几个填充。然后我搜索了问题并找到解决方案" ContentInset"。但我的应用程序仍在显示填充。

代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/LinearLayout01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/a_color_bg_hash"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:background="#fff"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:contentInsetRight="0dp"
            app:contentInsetEnd="0dp">


            <RelativeLayout
                android:id="@+id/RelativeLayout01"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:id="@+id/mytextview1"
                    android:layout_width="178.3dp"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:layout_height="match_parent"
                    android:src="@drawable/title1"
                    android:layout_alignParentStart="true" />

                <View
                    android:id="@+id/line"
                    android:layout_width="1dp"
                    android:layout_height="match_parent"
                    android:layout_alignStart="@+id/close_btn"
                    android:background="@color/a_color_bg_hash" />

                <ImageButton
                    android:layout_width="80dp"
                    android:id="@+id/close_btn"
                    android:layout_height="match_parent"
                    android:layout_alignParentEnd="true"
                    android:src="@drawable/ic_close"
                    android:background="@android:color/transparent"
                    android:padding="20dp"
                    android:layout_centerVertical="true"
                    android:scaleType="fitXY"/>

            </RelativeLayout>

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

    </android.support.design.widget.AppBarLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

我建议您不要在Toolbar

中使用任何布局

这是我的工具栏工作正常

<强> toolbar_layout.xml

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:layout_alignParentTop="true"
        android:background="@color/primary"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/home_layout_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/primary"
            android:minHeight="?attr/actionBarSize"
            app:contentInsetStart="0dp"
            app:contentInsetLeft="0dp">

            <com.cdnsol.cdnshow.widget.StyledTextView
                android:id="@+id/title_tv"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:ellipsize="end"
                android:gravity="center"
                android:layout_gravity="center"
                android:maxLines="1"
                android:padding="@dimen/_10sdp"
                android:text="@string/app_name"
                android:textColor="@color/white_color"
                android:textSize="@dimen/_16sdp" />

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

将您的代码与此匹配,希望它能为您提供帮助。

修改

使用当前代码尝试使用此

替换RelativeLayout的属性
<RelativeLayout
            android:id="@+id/RelativeLayout01"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_gravity="center">

答案 1 :(得分:0)

我找到了一个临时解决方案。那就是删除Toolbar及其布局并使用我们自己的自定义布局。它就像一个Toolbar。然后我们不必担心填充或边缘问题。

谢谢。