简体   繁体   English

工具栏在那里时,带有片段的android布局中断

[英]android layout with fragment breaks when toolbar is there

Couldn't find a more appropriate title but my problem is the following. 找不到更合适的标题,但我的问题如下。 I have a LinearLayout that contains another LinearLayout and a fragment. 我有一个LinearLayout ,其中包含另一个LinearLayout和一个片段。 I also want to add the toolbar but when I do so, then I only see the toolbar and the other screen is just white. 我也想添加toolbar但是这样做时,我只会看到toolbar ,而另一个屏幕只是白色。

Here is the layout: 这是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="xeniasis.mymarket.MainActivity">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false"
        android:weightSum="4">

        <LinearLayout
            android:id="@+id/categories_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:paddingRight="20dp">

            <ExpandableListView
                android:id="@+id/categories_listView"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>

        <fragment
            android:id="@+id/mainPane"
            android:name="xeniasis.mymarket.Products"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            tools:layout="@layout/activity_main" />
    </LinearLayout>
</LinearLayout>

Toolbar: 工具列:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:elevation="4dp"
    android:padding="5dp"
    android:theme="@style/ToolbarTheme" />

And the fragment layout: fragment布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="xeniasis.mymarket.MainActivity">

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/swipeRefreshContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:paddingTop="@dimen/activity_vertical_margin">

            <GridView
                android:id="@+id/productsGridview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:numColumns="3"></GridView>

        </android.support.v4.widget.SwipeRefreshLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/settings"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|start"
            android:layout_margin="8dp"
            android:clickable="true"
            android:elevation="5dp"
            android:src="@android:drawable/ic_menu_search" />
    </android.support.design.widget.CoordinatorLayout>
</LinearLayout>

This only happens when I have a fragment , cause I previously had the same xml with a static layout where the fragment is now. 仅当我有一个fragment ,才会发生这种情况,因为以前我有一个具有静态布局的xml,而现在该fragment处于静态布局。

The problem is that you used a LinearLayout with 问题是您将LinearLayout与

android:orientation="horizontal"

and since the toolbar has match_parent as width it occupies all the screen. 由于工具栏的宽度为match_parent ,因此它占据了整个屏幕。 Try to use 尝试使用

android:orientation="vertical" 

in the root layout 在根目录中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM