Панели инструментов дочерний узел не центрируется - PullRequest
0 голосов
/ 07 мая 2018

Мне стыдно спрашивать об этом, но у меня немало проблем с этим, и я не могу понять, почему.

А именно, я пытаюсь центрировать LinearLayout внутри Панели инструментов, но я не могу понять, почему он не работает.

Может кто-нибудь увидеть, где я сделал ошибку?

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
     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="?attr/actionBarSize"
     android:background="@color/white"
     android:focusable="true"
     android:focusableInTouchMode="true"
     android:gravity="center"
     android:orientation="horizontal"
     android:id="@+id/toolbar">

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_gravity="center">
</LinearLayout>

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

Этот код приводит к следующему: картина проблемы

Ответы [ 2 ]

0 голосов
/ 07 мая 2018

Левая вставка ToolBar's contentInsetStart, которая имеет размер по умолчанию. Вы можете установить значение 0dp с xml.

<android.support.v7.widget.Toolbar
    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="?attr/actionBarSize"
    android:background="@color/white"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:gravity="center"
    app:contentInsetStart="0dp"
    android:orientation="horizontal"
    android:id="@+id/toolbar">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    </LinearLayout>

</android.support.v7.widget.Toolbar>
0 голосов
/ 07 мая 2018

Использование вида обертки для центрирования чего-либо внутри панели инструментов:

<android.support.v7.widget.Toolbar
 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="?attr/actionBarSize"
 android:background="@color/white"
 android:focusable="true"
 android:focusableInTouchMode="true"
 android:gravity="center"
 android:orientation="horizontal"
 android:id="@+id/toolbar">

 <FrameLayout
  android:layout_height="match_parent"
  android:layout_width="match_parent">
    <TextView 
        text="Something"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />
 </FrameLayout>

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