2016-07-25 49 views
0

我有我的滾動型這個令人沮喪的問題裏面,這裏是我的層次:安卓:與重量的LinearLayout滾動型

ScrollView (Vertical) 
    LinearLayout (Vertical) 
     ImageView weight= 0.5 
     Whatever weight= 0.1 
     Whatever weight= 0.2 
     Whatever weight= 0.2 

如果我刪除了滾動(並讓的LinearLayout作爲主項),這可以正常使用任何圖像:圖像佔用了屏幕大小的50%,而其他視圖則佔用其餘的視圖。

但是,當ScrollView在這裏時,如果我的圖像太高,「weight」參數將被完全忽略:圖像將盡其所能去適應屏幕寬度,然後顯然會過高並且需要更多比屏幕的50%。編輯:實際上,所有的「權重」屬性似乎忽略:

沒有滾動型: enter image description here

隨着滾動型: enter image description here

我想要的線性佈局,以配合得非常完美,而無需滾動。那可能嗎 ?我試圖改變一些圖像選項(scaleType,adjustViewBounds),但我沒有設法得到我想要的。

這裏是整個XML:

<?xml version="1.0" encoding="utf-8"?> 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fillViewport="true"> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="1"> 


    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.5" 
     android:adjustViewBounds="true" 
     android:src="@drawable/testing" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.2" 
     android:text="I'M A TEST" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.1" 
     android:text="I'M A TEST" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.2" 
     android:text="I'M A TEST" /> 

</LinearLayout> 
</ScrollView> 

注:我需要的滾動型,因爲我使用的是這樣一個PullToRefresh滾動型

+3

出於好奇,如果你不想滾動,爲什麼你需要scrollview? – wanpanman

+1

基本上這^。滾動查看沒有維度,它可以是無限的。你期望你的imageview有50%是什麼?如果它是線性佈局,它知道。 ScrollView佔用儘可能多的空間,因爲它需要。 – Vucko

+1

這看起來不對。看看https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html這可能是你需要的。 – tynn

回答

1

我解決它通過增加父母的線性佈局(和主要的RelativeLayout):

ScrollView (Vertical) 
     LineaLayout 
      LinearLayout (Vertical) 
       ImageView weight= 0.5 
       Whatever weight= 0.1 
       Whatever weight= 0.2 
       Whatever weight= 0.2 

而且孩子的LinearLayout的高度與childLinearLayout.getLayoutParams().height

-1

嘗試。

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

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

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:adjustViewBounds="true" 
     android:padding="50dp" 
     android:src="@drawable/ic_launcher" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:padding="20dp" 
     android:text="I&apos;M A TEST" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:padding="20dp" 
     android:text="I&apos;M A TEST" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:padding="10dp" 
     android:text="I&apos;M A TEST" /> 
</LinearLayout> 
</ScrollView> 
+0

但是這不會考慮50%,20%,20%,10%的高度要求。 –

+0

@Rotwang我有更改的代碼,所以請檢查它 –

+0

同上... –

0
設置程序屏幕的高度

也許是爲時已晚,但您的問題可以通過將android:fillViewport="true"添加到您的ScrollView來解決:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true" >