2017-07-16 34 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 –