2012-07-31 125 views
2

不管其他内容如何让按钮和其他内容保持固定在屏幕底部?在Android屏幕底部固定内容

我有这样一个布局:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/ScrollView01" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:fillViewport="true" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_gravity="center_horizontal" 
      android:layout_weight="1" 
      android:gravity="center_horizontal" > 

      <TextView ... /> 

      ... 

      <TextView ... /> 

     </LinearLayout> 

     <Button 
      android:id="@+id/ButtonMain" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" 
      android:layout_weight="1" 
      android:text="@string/button_text" 
      android:textSize="30px" 
      android:textStyle="bold" 
      android:width="200px" /> 

    </LinearLayout> 

</ScrollView> 

而且我想ButtonMain的底部总是在屏幕的底部。但是,该按钮呈现在线性布局内容的正下方,而不是在屏幕的底部。我试过layout_gravity="center_horizontal|bottom",以及将按钮放置在自己的LinearLayout中,具有类似的重力,但是这不起作用。

我该如何实现这种布局?

+0

您是否试图静态渲染屏幕底部的'Button',尽管有'ScrollView'? – Eric 2012-07-31 19:17:33

+0

@Eric,确实。当我尝试将按钮放在ScrollView之外时,我收到了很多错误。 – Cerin 2012-07-31 20:20:07

回答

2

您可以使用RelativeLayout而不是LinearLayout。通过添加以下代码将结盟到屏幕上的按钮

android:layout_alignParentBottom="true" 
如果你想按钮总是在底部,你应该把滚动型的RelativeLayout的内部与按钮在其底部

看到: http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

+0

这只会导致所有元素在屏幕上伸展。 – Cerin 2012-07-31 22:47:09

+0

您可以在组件之间定义填充和边距 – 2012-08-01 17:11:13

1

裹滚动视图在一个相对布局,但在XML文件中第一个设置为true的alignParentBottom字段中的按钮。然后将scrollView放入并将其设置为按钮上方。然后滚动视图将填充剩下的空间,并始终在按钮上方,这些按钮将一直显示而不滚动。

1
<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scroller" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/txt1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="Hello" 
      android:textSize="25sp" /> 

     <TextView 
      android:id="@+id/txt2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="Hello2" 
      android:textSize="25sp" /> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:gravity="bottom|center" 
      android:orientation="horizontal" > 

      <Button 
       android:id="@+id/ButtonMain" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="5dp" 
       android:layout_marginRight="5dp" 
       android:text="click me" 
       android:textSize="30px" 
       android:textStyle="bold" /> 
     </LinearLayout> 
    </LinearLayout> 

</ScrollView> 
+0

这完全不起作用。该按钮完全消失... – Cerin 2012-07-31 20:28:15

+0

对不起,我编辑了我的答案,请你检查它并让我知道是否有任何问题? – Ali 2012-07-31 21:39:31

1

根据以上评论,OP希望在ScrollView之外的Button。但是,XML只能有一个根元素,因此您必须将ScrollViewButton包装在某种布局中。

我已经证明,下面,使用初始布局和尺寸:

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

    <ScrollView 
     android:id="@+id/ScrollView01" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:fillViewport="true" > 

     <!-- You can remove one of these two LinearLayouts, since they just nest each other. Which one you want to remove is up to you. --> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" > 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" 
       android:gravity="center_horizontal" > 

       <TextView ... /> 

       ... 

       <TextView ... /> 

      </LinearLayout> 

     </LinearLayout> 

    </ScrollView> 

    <Button 
     android:id="@+id/ButtonMain" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:text="@string/button_text" 
     android:textSize="30px" 
     android:textStyle="bold" 
     android:width="200px" /> 

</LinearLayout> 

让我知道,如果你有任何问题!