2016-08-11 99 views
0

我有一個問題,我無法找到任何答案。 如何製作佈局滾動並製作另一個不滾動的佈局? 另一方面,我想做一個佈局,包括一些按鈕,如果需要滾動,我想在屏幕的底部放兩個按鈕,我不希望它們在滾動時消失。如何滾動活動的一部分

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:background="@drawable/background" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     tools:context="com.myname.myapp.MainActivity"> 

    // a bunch of Buttons and TextViews 

    </RelativeLayout> 
</scrollView> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

     // two Moveless Buttons 

</RelativeLayout> 

但它不起作用。

+0

「不起作用」是什麼意思?當你運行這個應用程序會發生什麼,它與你想要的有什麼不同? –

+0

它有錯誤。 「多根標籤」 – Skillson

+0

谷歌搜索錯誤消息幾乎總是很好。例如,[this](http://stackoverflow.com/questions/24275533/multiple-root-tags-in-android-studio)。 –

回答

3

您的ScrollView的寬度和高度與父級匹配。這意味着它佔用了整個可用空間,不會爲RelativeLayout留下任何東西。您可能想要將已有的內容包裝到LinearLayout中,然後使用layout_weight屬性來分隔空間。

+0

是的。有用。非常感謝。 – Skillson

0

我不知道我明白你想做什麼。不過,試試這個,並給予反饋

(代碼示例Android Studio中未測試)

  <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
       > 
     <ScrollView 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:background="@drawable/background" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <RelativeLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        tools:context="com.myname.myapp.MainActivity"> 

       // a bunch of Buttons an TextViews 


       </RelativeLayout> 
      </ScrollView> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="80dp (for example)>" 
        android:layout_alignParentBottom="true"> 

        // two Moveless Buttons 

      </RelativeLayout> 
</RelativeLayout> 
+0

謝謝。你的解決方案有效正是我想要的。 – Skillson

0

讓你的根垂直的LinearLayout用滾動視圖,以保持滾動視圖和一個RelativeLayout的,牽你的按鈕。

您可以利用LinearLayout的加權功能,將滾動視圖的權重設置爲1,這樣當按鈕按住RelativeLayout時,它將佔用盡可能多的空間,這將成爲最大按鈕的大小。

下面是一個例子:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ScrollView 
     android:background="@drawable/background" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      tools:context="com.myname.myapp.MainActivity"> 

     // a bunch of Buttons an TextViews 

     </RelativeLayout> 
    </ScrollView> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

      // two Moveless Buttons 

    </RelativeLayout> 
</LinearLayout> 

HTHS!

相關問題