2012-03-07 94 views
0

我希望在我的活动屏幕底部保留一个按钮。无论上面的scrollview大小如何,它都必须被固定。问题是,一旦scrollview的文本浏览占据了一定的位置,我的按钮的高度就会不断下降,最终会被移出活动屏幕。这是我使用的布局。在屏幕底部始终放置一个固定高度的按钮

<?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:id="@+id/tvBanner" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" android:gravity="center" 
     android:text="Practice Exam" /> 


    <ScrollView android:id="@+id/Scroll" android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <LinearLayout android:id="@+id/LinearLayout1" 
      android:layout_width="match_parent" android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <TextView android:id="@+id/tvDesc" android:layout_width="fill_parent" 
       android:layout_height="wrap_content" android:layout_margin="10dp" 
       android:gravity="center" android:paddingTop="40dp" android:text="@string/welcom" 
       android:textSize="20sp" /> 

      <TextView android:id="@+id/tvURL" android:layout_width="wrap_content" 
       android:layout_height="wrap_content" android:layout_marginBottom="30dp" 
       android:paddingTop="10dp" android:layout_gravity="center" 
       android:text="hello" android:textColor="@android:color/white" 
       android:typeface="sans" /> 
     </LinearLayout> 
    </ScrollView> 

    <RelativeLayout android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:gravity="bottom"> 

     <Button android:id="@+id/btBottom" android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:layout_weight="1" 
      android:text="Enter" /> 

    </RelativeLayout> 



</LinearLayout> 

我也尝试在滚动视图中使用android:weight = 1和android:layout_height = 0dp。但是这从我的活动中移除了整个滚动视图部分,我什么也看不见。

我知道,有很多类似的问题询问了这个,并相信我,我尝试了很多这些。但是,这些技巧都没有为我工作。我花了将近半天的时间来解决这个问题。请帮助。

回答

2

对于这样的情况总是使用RelativeLayouts。 LinearLayout不适用于这种用法。

试试这个:

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"   
    android:layout_centerHorizontal="true"> 

    <Button android:id="@+id/btBottom" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Enter" 
     android:layout_alignParentBottom="true"/> 

    <ScrollView 
     ... 
     android:layout_above="@id/btnGetMoreResults"/> 
</RelativeLayour> 
+0

这似乎是工作..谢谢一吨! – ambit 2012-03-07 14:25:15

0

看起来像你需要 'alignParentBottom' 在你的按钮,像这样的

<Button android:id="@+id/btBottom" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:text="Enter" /> 
+0

已经尝试过。不幸的是,不起作用。 – ambit 2012-03-07 14:12:05

0
  1. 不要包裹的ScrollViewButtonRelativeLayout
  2. 设置layout_height到0dp
  3. 添加的ScrollView layout_weight 1
0

为此,它很可能是一个好主意,如果你使用的RelativeLayout的外包装布局。然后,你可以只设定按钮的布局里面的LinearLayout,并设置layout_alignParentButtom="true",同时您将@+id/Scrolllayout_alignParentTop="true"

此外,你应该设置与按钮的布局inside's高度wrap_content而不是fill_parent

0

为什么要在顶层使用LinearLayout? 选择与填充一个RelativeLayout /填写以下三个子女(没有进一步嵌套!):

1)的TextView与android:layout_alignParentTop="true"android:layout_centerHorizontal="true"

2)键,android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"

3)滚动型与android:[email protected]/textview_idandroid:layout_above="@id/button_id"

虽然我没有测试,但试试看。

1

你应该尝试使用,而不是线性RealativeLayout,然后你可以使用

android:layout_above="@+id/btBottom" 
android:layout_alignParentBottom="true" 

这应该解决您的问题。

+0

谢谢..一个类似的解决方案是由另一个人建议,它的工作.. – ambit 2012-03-07 14:30:58

相关问题