2011-12-29 47 views
0

我目前正在一个Android项目,我想要将屏幕拆分为三个部分。与滚动中心的三个组件用户界面

第一部分将其不移动定位在屏幕的顶部的标题。 第二部分是可滚动的主要内容 第三部分是固定在屏幕底部的两个按钮。

在此设置在顶部和底部会停留在屏幕上,只有屏幕的中心部分会滚动。

出于某种原因,当我试图做到这一点的顶部和中间做工精细,但其在按键从未显示的底部。

下面是我用来尝试得到这个工作

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/start_eulaTitle" 
      android:textSize="15dp" 
      android:textStyle="bold" /> 
     <ScrollView 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent"> 
      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/start_eula" /> 
     </ScrollView> 
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
     <Button android:id="@+id/start_btnAgree" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/button_agree" /> 
     </LinearLayout> 
    </LinearLayout> 

感谢您的帮助,您可以提供的代码。

回答

0

RelativeLayout可能是您完成所寻找任务的方式。喜欢的东西:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TextView 
     android:id="@+id/title_textview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/start_eulaTitle" 
     android:textSize="15dp" 
     android:textStyle="bold" /> 

    <ScrollView 
     android:id="scrolling_layout" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/button_layout" 
     android:layout_below="@+id/title_textview" > 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/start_eula" /> 
    </ScrollView> 

    <LinearLayout 
     android:id="@+id/button_layout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/start_btnAgree" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/button_agree" /> 

     <Button 
      android:id="@+id/start_btnAgree" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/button_agree" /> 
    </LinearLayout> 

</RelativeLayout> 

这基本上告诉的LinearLayout与按钮始终对齐到屏幕的底部,而标题的TextView将默认坐在左上角。可滚动区域被定义为保持在这两者之间:在标题下方但在按钮上方。

//编辑:如果你真的想坚持使用的LinearLayout作为根,你可能也做到以下几点:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/start_eulaTitle" 
     android:textSize="15dp" 
     android:textStyle="bold" /> 

    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/start_eula" /> 
    </ScrollView> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/start_btnAgree" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="@string/button_agree" 
      android:layout_weight="1"/> 

     <Button 
      android:id="@+id/start_btnAgree" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="@string/button_agree" 
      android:layout_weight="1" /> 
    </LinearLayout> 

</LinearLayout> 

,奇迹发生了由1的权重设置为滚动型。这将尽可能动态地拉伸视图,而不用将任何东西推到屏幕外。

+0

谢谢伟大的答案,很好的作品。 – Boardy 2011-12-29 21:54:27

0

问题是,您已经为中间和底部元素定义了android:layout_height="match_parent"(或"fill_parent",这是相同的)。从上到下进行布局,所有可用空间都被赋予滚动视图。 您应该查看android:layout_weight参数,或者您可以设置特定的layout_height,例如, 15dp之类,尽管这些绝对值对于Android来说并不是那么好(由于所有这些不同的屏幕尺寸和分辨率)。