2017-09-15 34 views
0

我想在屏幕頂部有一個「fixed_space」,屏幕底部應該有一個「scrollview」,應該在fixed_space下面。屏幕的其餘部分是一個容器「rest_space」。Android滾動查看內容大小應該是

不幸的是,如果內容太大/可滾動,我的滾動視圖的高度較低(fixed_space有100dp)。

我試圖用ConstraintLayout和RelativeLayout來實現,但是我得到了相同的結果。 任何想法爲什麼scrollview有100dp較短的高度,因爲它應該是?

編輯:ScrollView應該儘可能多的空間,她需要。

<?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"> 

    <View 
     android:id="@+id/fixed_space" 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:background="@android:color/holo_red_dark" /> 

    <View 
     android:id="@+id/rest_space" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="@android:color/black" /> 

    <ScrollView 
     android:id="@+id/scrollview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/holo_green_dark" 
     android:fillViewport="true"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="START OF SCROLLVIEW \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nMIDDLE OF SCROLLVIEW \n\n\n\n\n\n\n\n\n\n\n\nEND OF SCROLLVIEW" /> 

    </ScrollView> 
</LinearLayout> 
+0

你有沒有試圖消除「rest_space」認爲,應該是問題 –

+0

告訴我fixed_space,rest_space的比例和滾動型你想要什麼?。 –

+0

@PierGiorgioMisley ScrollView應該只佔用她需要的太多空間。如果我刪除rest_space,滾動視圖將在fixed_space和底部屏幕之間佔用整個空間。 – prosto

回答

0

試試這個:

<?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"> 

<View 
    android:id="@+id/fixed_space" 
    android:layout_width="match_parent" 
    android:layout_height="100dp" 
    android:background="@android:color/holo_red_dark" /> 


<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <View 
     android:id="@+id/rest_space" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/scrollview" 
     android:background="@android:color/black" /> 

    <ScrollView 
     android:id="@+id/scrollview" 
     android:layout_alignParentBottom="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/holo_green_dark" 
     android:fillViewport="true"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="START OF SCROLLVIEW \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nMIDDLE OF SCROLLVIEW \n\n\n\n\n\n\n\n\n\n\n\nEND OF SCROLLVIEW" /> 

    </ScrollView> 


</RelativeLayout> 


</LinearLayout> 
+0

如果我將文本設置爲android:text =「ScrollView的開始」,其中沒有rest_space,並且scrollview不在屏幕的底部,因爲她應該是。 – prosto

+0

我做了一個編輯。請嘗試。 –

+0

它的工作原理! 請在最後爲LinearLayout添加結束標記。 – prosto

0

好吧,我想這應該解決您的probem:

首先,我開始使用RelativeLayout的。我這樣做是爲了讓fixed_space對齊頂部,scrollview對齊底部。

然後,我改變視圖的LinearLayout中,並把其內部一個TextView,所以我可以用WRAP_CONTENT,這是棘手的部分:

當您使用滾動型與WRAP_CONTENT並用重的東西一景,Android的可由於沒有參考,所以不需要進行計算以獲得畫面。當我把TextView和wrap_content本身聯繫起來,因爲Android知道文本的大小。

您不必使用TextView,但您需要任何可以給Android提供參考的東西。

在這種情況下,scrollview將被拉伸,而不是LinearLayout。如果你希望第二個被拉開,那麼恐怕你需要爲你的滾動視圖設置一個固定的高度。

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

<View 
    android:id="@+id/fixed_space" 
    android:layout_width="match_parent" 
    android:layout_height="100dp" 
    android:layout_alignParentTop="true" 
    android:background="@android:color/holo_red_dark" /> 

<LinearLayout 
    android:id="@+id/rest_space" 
    android:layout_below="@+id/fixed_space" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/black"> 

    <TextView 
     android:text="Test" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

<ScrollView 
    android:id="@+id/scrollview" 
    android:layout_below="@id/rest_space" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/holo_green_dark" 
    android:layout_alignParentBottom="true"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="START OF SCROLLVIEW \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nMIDDLE OF SCROLLVIEW \n\n\n\n\n\n\n\n\n\n\n\nEND OF SCROLLVIEW" /> 

</ScrollView> 
</RelativeLayout> 
+0

謝謝你的回答。但是我可能對我的問題描述不夠。 ScrollView應該有一個動態高度。因此,如果滾動視圖中的TextView僅具有「滾動視圖開始」,則滾動視圖高度將僅爲一行高度,並將保留在屏幕底部。 「rest_space」高度將佔用「fixed_space」和「scrollview」之間的剩餘空間。 – prosto