2013-03-17 25 views
19

我有一個ViewPager和帶有選項卡的ActionBar。其中一個選項卡有一個ListView,當我點擊該ListView中的某個選項時,我添加了一個子片段,它是該行的詳細視圖。在ScrollView中查看與配置不匹配的父

該詳細視圖是一個帶有LinearLayout的ScrollView。 ScrollView是match_parent/match_parent,其中的LinearLayout是width=match_parentheight=wrap_content。在手機大小的模擬器上,細節視圖根據需要填充屏幕,但在平板電腦上,細節視圖僅覆蓋屏幕寬度的一部分...即使ScrollView的寬度和LinearLayout的寬度是兩者都是match_parent。

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/rootView" style="@style/RootScrollView"> 

    <LinearLayout android:id="@+id/scrollContentView" style="@style/ScrollViewContent"> 
     ... 
    </LinearLayout> 
</ScrollView> 

這裏是滾動視圖的樣式:

<style name="RootScrollView"> 
    <item name="android:layout_width">match_parent</item> 
    <item name="android:layout_height">match_parent</item> 
</style> 

下面是對內容的LinearLayout樣式:

<style name="ScrollViewContent"> 
    <item name="android:layout_width">match_parent</item> 
    <item name="android:layout_height">wrap_content</item> 
    <item name="android:paddingLeft">15dp</item> 
    <item name="android:paddingRight">15dp</item> 
    <item name="android:orientation">vertical</item> 
    <item name="android:background">@drawable/image_legal_paper_tile</item> 
</style> 

內容觀點已經重複背景image_legal_paper_tile它是本:

<?xml version="1.0" encoding="utf-8"?> 
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@drawable/legal_paper_tile" 
    android:gravity="fill_horizontal" 
    android:tileMode="repeat" /> 

因此,圖像應該水平拉伸並重復,從而在背景中創建一個黃色的法律紙墊。

這就是列表和詳細信息視圖看起來像手機模擬器:

enter image description here

這就是列表和詳細信息視圖看起來像一個真正的平板電腦。黃色的合法填充片段應該填充屏幕的整個寬度,但事實並非如此。

enter image description here

編輯:

這裏是背景的法律文件圖片:

enter image description here

這是320像素寬。手機模擬器的寬度爲480px,圖像正確伸展以填充屏幕寬度。然後按照預期垂直重複。但是,這並不是爲了填充平板電腦屏幕的寬度。

平板電腦上顯示的寬度不是圖像的原始大小,因爲它會根據內容更改大小。當片段第一次加載時,它是一個寬度。然後填寫字段並執行計算,在內容視圖的底部添加一些文本。該文本比現有內容更寬,因此設置文本時,片段的寬度會增加以支持更寬的內容。

因此,簡而言之,不是,平板電腦的寬度不是圖像的原始大小。圖像伸展,只是不能伸展。

+0

對所有佈局尺寸只使用一種佈局嗎?你的背景圖像有多大?平板電腦上顯示的背景圖像大小是什麼?然後它可以是一個瓦重複問題(爲此可能檢查此[鏈接](http://stackoverflow.com/questions/4077487/background-image-not-repeating-in-android-layout))。 – abbath 2013-03-17 21:20:20

+0

平板電腦上顯示的寬度不是圖像的原始大小,因爲它會根據內容更改大小。當片段第一次加載時,它是一個寬度。然後填寫字段並執行計算,在內容視圖的底部添加一些文本。該文本比現有內容更寬,因此設置文本時,片段的寬度會增加以支持更寬的內容。 – 2013-03-18 05:06:09

+0

如果您在滾動的子項的線性佈局中使用了佈局邊距,則應將其更改爲填充以正常工作。 – 2016-12-07 08:15:11

回答

68

上了滾動使用android:fillViewport="true"和了滾動android:height="wrap_content"的孩子。如果你想讓許多具有不同屬性的孩子成爲容器的主要孩子。將他設置爲wrap_content,並將其孩子設置爲match_parent。

例如:

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 

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

     <LinearLayout 
      android:id="@+id/dimrix_sub_child" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:visibility="gone" > 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/dimrix_sub_child_21" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:visibility="gone" > 
     </LinearLayout> 
    </LinearLayout> 
</ScrollView> 

就是這個例子,我可以爲每個孩子的代碼集可視性,並根據需要,他將匹配父。

+1

將fillViewport設置爲true幫助了我的情況。 – Scadge 2014-01-16 05:35:25

+5

謝謝!你剛剛救了我好幾個小時! – marienke 2014-08-21 14:05:49

+0

歡迎您( - : – 2014-11-18 10:44:44

0

我已經想出了一個解決方法,但我不會接受這個答案,並希望有人會找到真正的答案。

在我的列表視圖片段onListItemClick()處理函數中,我在細節片段上設置了幾個屬性(minWidth和minHeight)。然後在細節片段的onCreateView()方法中,我將最小寬度和高度設置爲該值。

細節片段,然後,適當填充的空間,並仍然允許滾動等

3

嘗試增加機器人:fillViewport =「true」以你的滾動型

記得機器人:‘設置高度爲母公司的高度’layout_height =」 FILL_PARENT」是指這顯然不是你希望在使用ScrollView時。畢竟,如果ScrollView的內容總是與自身一樣高,它將變得毫無用處。要解決此問題,您需要使用名爲android:fillViewport的ScrollView屬性。如果設置爲true,則此屬性會導致滾動視圖的子級擴展到ScrollView的高度(如果需要)。當孩子比ScrollView高時,該屬性不起作用。

相關問題