2016-08-11 92 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!

相关问题