我有以下布局:
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.View" />
<variable
name="imagelimit"
type="int" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="@bool/fitsSystemWindows">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_height"
android:background="@color/colorPrimary" />
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordlt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="@dimen/status_bar_height"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<include
android:id="@+id/incltoolbar"
layout="@layout/toolbar" />
<LinearLayout
android:id="@+id/baseLlt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="155dp"
android:gravity="center"
android:textSize="16sp"
android:textStyle="bold"
android:text="@string/no_pet_images_yet"
android:visibility="@{imagelimit == 0 ? View.VISIBLE : View.GONE}" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:gravity="center"
android:textSize="15sp"
android:text="@string/pet_images_limit"
android:visibility="@{imagelimit == 0 ? View.VISIBLE : View.GONE}" />
<GridView
android:id="@+id/imageGrdv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:numColumns="2"
android:visibility="@{imagelimit != 0 ? View.VISIBLE : View.GONE}" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/addImageFlbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/ic_add"
android:visibility="@{imagelimit != 1 ? View.VISIBLE : View.GONE}"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
</FrameLayout>
</layout>
我想在工具栏下面显示LinearLayout(@ + id / baseLlt),而不添加margin top属性(工具栏高度对于所有类型的设备都不相同)。是否有适当的解决方案来实现这一目标?
答案 0 :(得分:2)
您可以简单地将Toolbar
和LinearLayout
包裹在垂直LinearLayout
中,如下所示:
<FrameLayout 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="match_parent"
android:fitsSystemWindows="@bool/fitsSystemWindows">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_height"
android:background="@color/colorPrimary" />
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordlt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/status_bar_height"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/incltoolbar"
layout="@layout/toolbar" />
<LinearLayout
android:id="@+id/baseLlt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="155dp"
android:text="@string/no_pet_images_yet"
android:textSize="16sp"
android:textStyle="bold"
android:visibility="@{imagelimit == 0 ? View.VISIBLE : View.GONE}" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="20dp"
android:text="@string/pet_images_limit"
android:textSize="15sp"
android:visibility="@{imagelimit == 0 ? View.VISIBLE : View.GONE}" />
<GridView
android:id="@+id/imageGrdv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="0dp"
android:numColumns="2"
android:verticalSpacing="0dp"
android:visibility="@{imagelimit != 0 ? View.VISIBLE : View.GONE}" />
</LinearLayout>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/addImageFlbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/ic_add"
android:visibility="@{imagelimit != 1 ? View.VISIBLE : View.GONE}"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
</FrameLayout>
答案 1 :(得分:0)
Try this code
<FrameLayout 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="match_parent"
android:fitsSystemWindows="@bool/fitsSystemWindows">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_height"
android:background="@color/colorPrimary" />
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordlt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/status_bar_height"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/incltoolbar"
layout="@layout/toolbar" />
<LinearLayout
android:id="@+id/baseLlt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="155dp"
android:text="@string/no_pet_images_yet"
android:textSize="16sp"
android:textStyle="bold"
android:visibility="@{imagelimit == 0 ? View.VISIBLE : View.GONE}" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="20dp"
android:text="@string/pet_images_limit"
android:textSize="15sp"
android:visibility="@{imagelimit == 0 ? View.VISIBLE : View.GONE}" />
<GridView
android:id="@+id/imageGrdv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="0dp"
android:numColumns="2"
android:verticalSpacing="0dp"
android:visibility="@{imagelimit != 0 ? View.VISIBLE : View.GONE}" />
</LinearLayout>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/addImageFlbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/ic_add"
android:visibility="@{imagelimit != 1 ? View.VISIBLE : View.GONE}"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
</FrameLayout>
答案 2 :(得分:0)
将xml属性添加到CoordinatorLayout和工具栏
android:fitsSystemWindows="true"
还将xml属性添加到linearlayout
app:layout_behavior="@string/appbar_scrolling_view_behavior"
<强> XML 强>
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="@bool/fitsSystemWindows"
xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_height"
android:background="@color/colorPrimary" />
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordlt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="@dimen/status_bar_height"
android:fitsSystemWindows="true"
>
<include
android:id="@+id/incltoolbar"
layout="@layout/toolbar" />
<LinearLayout
android:id="@+id/baseLlt"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:background="@color/colorPrimary"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="155dp"
android:gravity="center"
android:textSize="16sp"
android:textStyle="bold"
android:text="@string/no_pet_images_yet"
android:visibility="@{imagelimit == 0 ? View.VISIBLE : View.GONE}" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:gravity="center"
android:textSize="15sp"
android:text="@string/pet_images_limit"
android:visibility="@{imagelimit == 0 ? View.VISIBLE : View.GONE}" />
<GridView
android:id="@+id/imageGrdv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:numColumns="2"
android:visibility="@{imagelimit != 0 ? View.VISIBLE : View.GONE}" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/addImageFlbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/ic_add"
android:visibility="@{imagelimit != 1 ? View.VISIBLE : View.GONE}"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
</FrameLayout>
希望它有所帮助!!!!