2012-03-07 122 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

相關問題