活动中的片段内容大小不合适

时间:2018-04-01 00:17:24

标签: java android android-fragments

我在BottomNavigationView的活动中使用片段,开箱即用我注意到fragment内容低于BottomNavigationView

这是Android studio中的片段,然后在活动中对其进行充气:

this is the fragment in Android studio before inflating it in the activity

片段膨胀后的应用

the app after the fragment has been inflating

以下是我的片段XML我在此网站上的其他帖子中读到,我需要使用CoordinatorLayout作为父级,并将app:layout_behavior="@string/appbar_scrolling_view_behavior"添加到第一个孩子,但它不起作用

<android.support.design.widget.CoordinatorLayout
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_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">



<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:orientation="vertical">
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

<android.support.constraint.ConstraintLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  app:layout_behavior="@string/appbar_scrolling_view_behavior"
  android:layout_margin="16dp"
  tools:context="com.brigafrica.koyako.fragments.SendFragment">




<TextView
    android:id="@+id/textView"
    style="@android:style/TextAppearance.Small"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="36dp"
    android:text="@string/amount_to_send"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<android.support.v7.widget.AppCompatEditText
    android:id="@+id/edtAmountToSend"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:digits="0123456789.,"
    android:inputType="numberDecimal"
    app:layout_constraintBottom_toTopOf="@+id/appCompatButton"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView"
    app:layout_constraintVertical_bias="0.095" />


<android.support.v7.widget.AppCompatButton
    android:id="@+id/appCompatButton"
    style="@style/AppTheme.RoundedCornerMaterialButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:layout_margin="10dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:text="@string/send"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />



    </android.support.constraint.ConstraintLayout>
    </LinearLayout>
    </android.support.design.widget.CoordinatorLayout>

1 个答案:

答案 0 :(得分:0)

确定问题的必要条件是您的活动XML内容以及您如何管理活动中的片段。

最简单的思考方式是片段只是一个将在您的活动中显示的内容,您应该在活动中提供有关此片段尺寸的正确信息。

用我的例子解释

名为 MainActivity 的活动,其中包含一个 ViewPager 和一个 BottomNavigationView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="newidea.com.br.bottomnavigationexample.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/main.viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_navigation"
        android:layout_alignParentTop="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />


    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:itemBackground="@color/colorPrimary"
        app:itemIconTint="@drawable/nav_item_color_state"
        app:itemTextColor="@drawable/nav_item_color_state"
        app:menu="@menu/bottom_navigation_main" />

</RelativeLayout>

ViewPager是片段将在MainActivity中显示的部分,因此我必须告诉活动ViewPager不能入侵BottomNavigationView空间,否则将在ViewPager中显示的片段内容将低于BottomNavigationView。

怎么做?

在我的示例中,我使用以下方法配置了ViewPager:

android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom_navigation"
android:layout_alignParentTop="true"

这意味着ViewPager的宽度和高度与父级的宽度和高度匹配,ViewPager顶部与父级的顶部对齐,它是BottomNavigationView上方的布局,因此ViewPager无法入侵BottomNavigationView空间。

您需要了解的是,无论您的片段中的内容是什么,这里真正重要的是ViewPager的维度配置,因为ViewPager将显示片段。

Full example with 3 fragments, each one with a Button in the Bottom

Fragment in Android Studio before inflating

The app after the fragment has been inflated