2017-07-16 64 views
1

直接切入追踪。我想为我的应用程序有一个底部导航栏,然后在顶部有一个工具栏,中间是将放置内容的滚动视图。底部导航栏通过滚动视图布局被推出视图

现在的问题是,ScrollView布局将导航栏推出视图,现在导航栏不会显示。

As you can see, the navigation bar isn't showing because the ScrollView takes up the bottom of the screen too.

另一种解决方法我试过,但事实证明,在导航栏上的滚动型

That blue bar below the Home toolbar is the navigation bar.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="polytechnic.temasek.bluebeatsmusicapplication.HomePageActivity"> 

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

    <TextView 
     android:id="@+id/toolbartitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="Home" 
     android:textColor="@android:color/white" 
     android:textSize="32sp" 
     android:textStyle="bold" /> 
</android.support.v7.widget.Toolbar> 


<ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="539dp" 
    > 

     <android.support.constraint.ConstraintLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      tools:layout_editor_absoluteX="8dp" 
      tools:layout_editor_absoluteY="0dp"> 

      [Content inside] 
      </android.support.constraint.ConstraintLayout> 

    </ScrollView> 

<android.support.design.widget.BottomNavigationView 
    android:id="@+id/the_bottom_navigation" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:layout_gravity="bottom" 
    android:background="@color/colorPrimary" 
    app:itemBackground="@color/colorPrimary"> 
    </android.support.design.widget.BottomNavigationView> 
</LinearLayout> 

顶部弹出到目前为止,我只能够将滚动视图的高度暴力强制为某个特定的dp,以便为导航栏腾出空间,但是必须有另一种我应该丢失的方式?

回答

0

你可以更好地利用相对布局约束布局我建议使用约束布局)来处理这种情况。

我已将您的布局修改为约束布局。 我已经欺骗了滚动条的marginBottom值到bottomNavigationBar的高度[否则,scrollView的一些内容保留在bottomNavBar的后面]。

enter image description here

<android.support.constraint.ConstraintLayout 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.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:background="#000" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent"> 

     <TextView 
      android:id="@+id/toolbartitle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:text="Home" 
      android:textColor="@android:color/white" 
      android:textSize="32sp" 
      android:textStyle="bold" /> 

    </android.support.v7.widget.Toolbar> 

    <ScrollView 
     android:id="@+id/scrollView2" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_marginBottom="50dp" 
     android:layout_marginTop="0dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintHorizontal_bias="1.0" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/toolbar"> 

     <android.support.constraint.ConstraintLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintRight_toRightOf="parent"> 

      <TextView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:text="@string/long_text" 
       app:layout_constraintBottom_toBottomOf="parent" 
       app:layout_constraintHorizontal_bias="0.0" 
       app:layout_constraintLeft_toLeftOf="parent" 
       app:layout_constraintRight_toRightOf="parent" 
       app:layout_constraintTop_toTopOf="parent" /> 

     </android.support.constraint.ConstraintLayout> 
    </ScrollView> 

    <android.support.design.widget.BottomNavigationView 
     android:id="@+id/the_bottom_navigation" 
     android:layout_width="0dp" 
     android:layout_height="50dp" 
     android:layout_gravity="bottom" 
     android:background="@color/colorPrimary" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintHorizontal_bias="0.0" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent"> 

    </android.support.design.widget.BottomNavigationView> 


</android.support.constraint.ConstraintLayout> 
+0

你好,这个解决方案工作得很好,但我想知道为什么你使用的LinearLayout来约束或相对变化? – MechaKondor

+0

[约束布局](https://developer.android.com/training/constraint-layout/index.html)与其他布局相比具有许多优势:*更好地控制您的视图。 *较少嵌套布局。 *少写代码。 (大部分工作都可以在Layout编辑器中完成。)以及更多...只需浏览一些教程,您一定会发现它更容易(看起来很复杂;))。接受答案,如果它的工作; P –