пустое пространство осталось даже после установки match_parent - PullRequest
0 голосов
/ 14 декабря 2018

Я создал панель инструментов с панелью навигации и меню, теперь я пытаюсь добавить textView и edittext на той же панели инструментов и разместить их в центре, но проблема заключается в том, когда я добавляю относительную компоновку и устанавливаю ширину как соответствующуюродитель, он освобождает место слева, и элементы, которые должны быть размещены в центре, сдвинуты вправо.

Я вставил следующий код: -

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:theme="@style/AppTheme.AppBarOverlay">


<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/colorPrimary">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="H E A D"
            android:textSize="30dp"
            android:layout_centerHorizontal="true"
            android:textStyle="bold"
            android:id="@+id/ToolbarTextView"
            style="@style/Base.TextAppearance.AppCompat.Large"
            android:layout_marginTop="9dp"
            android:textColor="@color/colorwhite"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ToolbarTextView"
            android:layout_centerHorizontal="true"
            android:text="Tagline"
            android:textSize="15dp"
            android:textColor="@color/colorwhite"/>

        <EditText
            android:layout_width="292dp"
            android:layout_height="wrap_content"
            android:background="@drawable/main_search"
            android:padding="7dp"
            android:backgroundTint="@color/colorwhite"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="10dp"
            android:hint="Search..."
            android:id="@+id/ToolbarSearch1"/>

    </RelativeLayout>

</android.support.v7.widget.Toolbar></android.support.design.widget.AppBarLayout><include layout="@layout/content_main" /><android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />

xml код mainActivity: -

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.strease.user.strease.MainActivity"
android:id="@+id/layout4"
tools:showIn="@layout/app_bar_main">


<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:paddingTop="5dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/></android.support.constraint.ConstraintLayout>

ниже приведено эталонное изображение: -

Reference image

Снимок экрана устройстваприведены ниже: -

screenshot

PS: - мне нужно опустить пустое пространство и получить editText и textViews в центр макета (в настоящее времясмещены вправо)

Код навигационного ящика в основном задании: -

  <android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"><include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer"/></android.support.v4.widget.DrawerLayout>

Ответы [ 3 ]

0 голосов
/ 14 декабря 2018
android:fitsSystemWindows="false"
0 голосов
/ 14 декабря 2018
app:contentInsetEnd="0dp"  
app:contentInsetLeft="0dp"  
app:contentInsetRight="0dp"  
app:contentInsetStart="0dp"

Попробуйте добавить четыре строки на панели инструментов.Это решит вашу проблему.И сделайте ваш дочерний макет внутри ширины match_parent и высоты wrap_content.И установите ваш дочерний дочерний центр выравнивания вида горизонтально.Это решит вашу проблему.

Если проблема по-прежнему не решена, то обновите XML-файл для вашего макета точно с помощью включенного макета, чтобы мы могли выяснить, в чем именно заключается проблема с вашим макетом.

0 голосов
/ 14 декабря 2018

Вам необходимо добавить это свойства к Панели инструментов , чтобы удалить это пространство.

android:contentInsetEnd="0dp"
android:contentInsetLeft="0dp"
android:contentInsetRight="0dp"
android:contentInsetStart="0dp"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"

Как , как показано ниже.

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

Это удалит оставленное по умолчанию пространство слева от Панель инструментов .

Вот снимок экрана.Кажется Редактировать текст , показывающий точно центр из Панель инструментов .Также обновите ваш minSdkVersion до 21 , конечно, он будет работать.

Вот код layout .

<android.support.design.widget.AppBarLayout 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="130dp"
    android:background="@android:color/holo_red_light"
    android:theme="@style/AppTheme.AppBarOverlay">


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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/ToolbarTextView"
                style="@style/Base.TextAppearance.AppCompat.Large"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="9dp"
                android:text="H E A D"
                android:textColor="@android:color/white"
                android:textSize="30dp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/ToolbarTextView"
                android:layout_centerHorizontal="true"
                android:text="Tagline"
                android:textColor="@android:color/white"
                android:textSize="15dp" />

            <EditText
                android:id="@+id/ToolbarSearch1"
                android:layout_width="292dp"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="10dp"
                android:background="@android:color/white"
                android:backgroundTint="@android:color/white"
                android:hint="Search..."
                android:padding="7dp" />

        </RelativeLayout>

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


    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.AppBarLayout>

РЕДАКТИРОВАТЬ

Добавить еще одно свойство на панель инструментов для Значок навигации .

app:contentInsetStartWithNavigation="0dp"

Теперь ваша панель инструментов должно быть, как показано ниже.

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?attr/colorAccent"
        android:contentInsetEnd="0dp"
        android:contentInsetLeft="0dp"
        android:contentInsetRight="0dp"
        android:contentInsetStart="0dp"
        app:contentInsetEnd="0dp"
        app:contentInsetLeft="0dp"
        app:contentInsetRight="0dp"
        app:contentInsetStartWithNavigation="0dp"
        app:contentInsetStart="0dp">

РЕДАКТИРОВАТЬ 2

Я внес некоторые изменения в макет используется линейный макет вместо Относительная компоновка .

<android.support.design.widget.AppBarLayout 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="130dp"
    android:background="@android:color/holo_red_light"
    android:theme="@style/AppTheme.AppBarOverlay">


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?attr/colorAccent"
        android:contentInsetEnd="0dp"
        android:contentInsetLeft="0dp"
        android:contentInsetRight="0dp"
        android:contentInsetStart="0dp"
        app:contentInsetEnd="0dp"
        app:contentInsetLeft="0dp"
        app:contentInsetRight="0dp"
        app:contentInsetStartWithNavigation="0dp"
        app:contentInsetStart="0dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/ToolbarTextView"
                style="@style/Base.TextAppearance.AppCompat.Large"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:text="H E A D"
                android:textColor="@android:color/white"
                android:textSize="30dp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tagline"
                android:layout_marginTop="2dp"
                android:textColor="@android:color/white"
                android:textSize="15dp" />

            <EditText
                android:id="@+id/ToolbarSearch1"
                android:layout_width="292dp"
                android:layout_height="wrap_content"
                android:background="@android:color/white"
                android:layout_marginTop="3dp"
                android:backgroundTint="@android:color/white"
                android:hint="Search..."
                android:padding="7dp" />

        </LinearLayout>

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


    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.AppBarLayout>

enter image description here

...