如何垂直对齐不同高度的 3 个视图

How to align 3 views vertically with different heights

我有一个主 activity,它有 3 个视图:toolbarMainContainer& bottombar

这个问题,bottombar 根本不显示,一旦 MainContainer 设置为 match-parent,它就会填充布局的其余部分并推动 bottombar超出可见区域!如果高度设置为 wrap-content 同样的事情!那么如何解决这个问题..!?

<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">


  <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:background="@color/accent"/>

  <FrameLayout
    android:id="@+id/MainContainer"
    android:layout_below="@id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"/>

  <com.roughike.bottombar.BottomBar
    android:id="@+id/bottombar"
    android:background="@color/black"
    android:layout_below="@id/MainContainer"
    android:layout_width="match_parent"
    android:layout_height="50dp"/>


</RelativeLayout>

你应该使用

 android:layout_alignParentBottom="true"  

If true, makes the bottom edge of this view match the bottom edge of the parent. Accommodates bottom margin.

试试

<com.roughike.bottombar.BottomBar
    android:id="@+id/bottombar"
    android:background="@color/black"
    android:layout_below="@id/MainContainer"
    android:layout_width="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_height="50dp"/>

仅供参考

在您的 Framelayout 部分添加 android:layout_marginBottom="50dp"

MainContainer height and width = match parent and marign_bottom in mainContainer 50dp thats the height of bottom bar

<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">


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:background="@color/accent"
        android:layout_height="70dp"/>

    <FrameLayout
        android:id="@+id/MainContainer"
        android:layout_below="@id/toolbar"
        android:layout_width="match_parent"
        android:background="@color/colorPrimary"
        android:layout_height="match_parent"
        android:layout_marginBottom="50dp"/>

    <com.roughike.bottombar.BottomBar
        android:layout_alignParentBottom="true"
        android:id="@+id/bottombar"
        android:layout_below="@id/MainContainer"
        android:layout_width="match_parent"
        android:background="@color/black"
        android:layout_height="50dp"/>


</RelativeLayout>

这对我有用

<?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="com.truiton.bottomnavigation.MainActivity">
  <include
    android:id="@+id/mainToolbar"
    layout="@menu/toolbar" />

  <FrameLayout
    android:id="@+id/mainFrameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/mainToolbar"
    android:layout_above="@+id/mainBottomNavigation"
    />

  <android.support.design.widget.BottomNavigationView
    android:id="@+id/mainBottomNavigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_gravity="start"
    android:background="@color/bottom_navigation_view_background"
    app:itemIconTint="@menu/nav_item_color_state"
    app:itemTextColor="@menu/nav_item_color_state"
    app:menu="@menu/bottom_navigation_main"
    />

</RelativeLayout>

更好的解决方案是使用垂直方向的 LinearLayout 并为其子项设置权重。试试下面的代码。

<LinearLayout
  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:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">


  <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:background="@color/accent"/>

  <FrameLayout
    android:id="@+id/MainContainer"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@color/colorPrimary"/>

  <com.roughike.bottombar.BottomBar
    android:id="@+id/bottombar"
    android:background="@color/black"
    android:layout_width="match_parent"
    android:layout_height="50dp"/>

</LinearLayout>