2011-10-10 75 views
9

這是示例XML如何防止其他View被擠出屏幕的

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout android:layout_width="match_parent" 
     android:layout_height="wrap_content" android:orientation="vertical" 
     android:layout_margin="5dip" android:background="#FF992222"> 
     <ScrollView android:layout_height="wrap_content" 
      android:layout_width="fill_parent"> 
      <LinearLayout android:layout_height="wrap_content" 
       android:orientation="vertical" android:layout_width="fill_parent"> 
       <TextView android:layout_width="wrap_content" android:text="TextView" 
        android:layout_height="wrap_content" android:textSize="24dip"></TextView> 
      </LinearLayout> 
     </ScrollView> 
    </LinearLayout> 
    <LinearLayout android:layout_width="match_parent" 
     android:layout_height="wrap_content" android:orientation="vertical" 
     android:layout_margin="5dip" android:background="#FF555555"> 
     <TextView android:layout_width="wrap_content" android:text="TextView" 
      android:layout_height="wrap_content" android:textSize="24dip"></TextView> 
    </LinearLayout> 
</LinearLayout> 

的結果是這樣的:

enter image description here

如果你注意到,在上LinerLayout文被包裹在LinearLayout和ScrollView中。

如果上的內容不斷增加,它看起來像這樣

enter image description here

底部的LinearLayout會被排擠出屏幕的滾動型前被激活,使第一內容成爲滾動....而且我不希望被髮生了......

我應該怎麼做,使滾動型推前下方查看了屏幕是這樣的:

(這是編輯的圖像而不是由Android佈局編輯器創建)

enter image description here

希望這個問題不夠清楚..謝謝:)

+0

你爲什麼不保持一個滾動視圖裏面的一切。 –

+0

所以你不想讓灰色文本視圖移動,而紅色的文字視圖保持滾動,我是否正確? – Urban

回答

3

好吧,如果我理解你清楚的問題,你想在灰色的TextView保持靜止,而紅色textViews可以滾動左右,糾正我,如果我錯了這裏。如果這就是你想要的,那麼你可以使用RelativeLayout來實現。以下是產生如下輸出的代碼:
http://imageshack.us/photo/my-images/836/device20111011012652.png/
ListView條目是可滾動的,而按鈕(在你的情況下它應該是一個TextView)保持它的位置。

代碼:

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

<ListView android:id="@android:id/list" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_above="@+id/newtask" android:layout_alignParentTop="true"> 
</ListView> 

<Button android:layout_width="fill_parent" 
android:id="@+id/newtask" 
android:layout_height="wrap_content" 
android:text="Add New Task" android:layout_alignParentBottom="true"> 
</Button> 
</RelativeLayout> 

只要改變底部按鈕,一個TextView,它會匹配你想要的。

+0

哇太棒了....那不是我想要的答案...但它真的讓我想到如何RelativeLayout可以做...:) 在你的xml中,當android:layout_alignParentBottom =「true」被調用時,relativeLayout變成fill_parent,我不想。 相反,我擺脫它,並添加'android:layout_below =「@ + id/box1'到底部框,使它像我想... :) –

+0

哦不...它不能解決我的問題....這我想實現...我想第一個盒子變得靈活...所以當第一個盒子內容只有1/2行時,它會看起來像第一個圖片,當內容太多時,它會看起來像第三張圖片...... –

+1

確定這可能有點不過要實現,但是如果您使用的是ListView(理想情況下您應該使用您的情況),那麼您可以重寫onScroll偵聽器並檢查列表中的最後一項是否爲如果它是可見的,則必須以編程方式將灰色TextView的對齊方式設置爲最後一項的底部,否則將對齊方式設置爲屏幕底部(如答案中所示)。 listveiw:http://stackoverflow.com/questions/5123675/find-out-if-listview-is-scrolled-to-the-bottom – Urban

4

試試這個。關鍵是在線性佈局中使用layout_weight。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ScrollView android:layout_height="0dp" 
     android:layout_weight="1" 
     android:layout_width="fill_parent"> 
     .... 
    </ScrollView> 
    <TextView android:layout_width="fill_parent" android:text="TextView" 
     android:layout_height="wrap_content" android:textSize="24dip"/> 
</LinearLayout> 
+0

我知道我可以通過layout_weight實現最後的屏幕截圖。但是我想讓第一個盒子變得靈活..如果內容只有1/2線條,它會保持很小,並且當內容變得太多時可以滾動...... –

+0

如果更改'android: layout_height =「fill_parent」'到'android:layout_height =「wrap_content」'在第一個LinearLayout中。我認爲。 –

+0

爲我工作..謝謝 –

7

我花了大量的時間來解決這個問題,網上沒有任何信息,但我終於明白了。請享用!

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/mainlayout" 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
android:layout_gravity="center_vertical" 
android:orientation="vertical" 
> 

<LinearLayout 
    android:id="@+id/LL" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical"     
    android:gravity="center_vertical" 
    android:layout_centerVertical="true" 
    android:orientation="vertical" 
    android:weightSum="1" > 


    <ScrollView 
     android:id="@+id/scrollView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/TV1" 
     android:layout_margin="5dp" 
     android:layout_weight="1" 
     android:fillViewport="true" > 

     <TextView 
      android:id="@+id/TV2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" /> 
    </ScrollView> 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="5dp" 
     android:layout_weight="0" 
     android:orientation="horizontal" > 

     <TextView android:layout_width="wrap_content" android:text="TextView" 
     android:layout_height="wrap_content" android:textSize="24dip"></TextView> 

    </LinearLayout> 
</LinearLayout> 

+1

這應該是公認的答案,因爲它的工作原理。 –