【问题标题】:How to align 3 views vertically with different heights如何以不同的高度垂直对齐 3 个视图
【发布时间】:2018-01-14 08:58:30
【问题描述】:

我的主要活动有 3 个视图:toolbarMainContainer& bottombar

  • toolbar 的高度为 60dp
  • MainContainermatch-parent
  • bottombar50dp

问题在于,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 xml android-layout android-linearlayout android-relativelayout


    【解决方案1】:

    你应该使用

     android:layout_alignParentBottom="true"  
    

    如果为真,则使该视图的底边与 父母。容纳下边距。

    试一试

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

    【讨论】:

    • @SamZar Framelayout 部分添加android:layout_marginBottom="50dp"
    • 它有效,谢谢 :D 但您也应该通过添加此行来编辑答案android:layout_marginBottom="50dp"
    【解决方案2】:

    MainContainer 高度和宽度 = 匹配 mainContainer 中的 parent 和 marign_bottom 50dp 即底栏的高度

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

    【讨论】:

      【解决方案3】:

      这对我有用

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

      【讨论】:

      • 用户使用com.roughike.bottombar.BottomBar
      • 好的,但是对齐和布局设置应该是一样的。
      【解决方案4】:

      更好的解决方案是使用具有垂直方向的 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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-09
        • 2014-07-21
        • 2014-03-30
        • 2014-02-01
        • 2011-05-02
        • 2015-04-07
        相关资源
        最近更新 更多