2017-02-14 32 views
0

我正在AndroidStudio中執行一個Android項目,其中一個ScrollView包含一個垂直的LinearLayout,在該LinearLayout中有一個ImageView和另一個LinearLayout。我試圖將ImageView設置爲比LinearLayout小10倍,結果在預覽窗口中顯示正確,但在我的手機(帶有API23的Xperia Z3)上不顯示。XML佈局權重不工作在android API23上的滾動視圖

This is what I see in the preview window and what I want to achieve

但我的手機上的圖片充滿屏幕和圖像高度的寬度是成正比的原始圖像(幾乎佔滿半個屏幕,所以遠離它應該根據權重是什麼)

該應用的工作原理與Google API Pixel 7.1.1 API17一樣。

下面是代碼的一部分:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:fillViewport="true" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     <ImageView 
      android:layout_weight="1" 
      android:adjustViewBounds="true" 
      android:layout_height="0dp" 
      android:layout_gravity="center" 
      android:layout_width="wrap_content" 
      android:id="@+id/image" 
      android:scaleType="fitCenter" 
      android:src="@drawable/bjj2" /> 
     <LinearLayout 
      android:layout_weight="10" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="0dp"> 
      <LinearLayout 
       ...rest of the app 
     </LinearLayout> 
    </LinearLayout> 
</ScrollView> 

下面是完整的代碼https://github.com/IvarsGoldingen/Bjjscorekeeper/blob/master/app/src/main/res/layout/activity_main.xml

有人能告訴我什麼,我做錯了。

回答

0

我們應該瞭解尺寸是如何計算的。 讓我們一個一個來看看。

1)ScrollViewfill_parent/fill_parent(實際上是一個可以爲fill_parent更改爲match_parent/match_parent是據我記得過時,但它並沒有在這種情況下重要)。 因此,ScrollView顯示適合整個屏幕(寬度和高度)。

2)LinearLayout裏面的ScrollViewmatch_parent/wrap_content。這是正確的。您應該使用wrap_content獲取ScrollView以內的觀看次數。 原因 - 是ScrollView用於包裝視圖與通常未知的高度(如列表)。想象這裏match_parent作爲高度 - 高度LinearLayout將根據滾動視圖的高度來計算,這是整個屏幕的高度,並且屏幕高度可以高於屏幕高度。只是不合邏輯。但直到現在一切都還好。繼續。

3)LinearLayout內的物品。您希望它們是父級佈局高度的1/109/10。另外,您將內部項目的height設置爲0,以表明您要從weights計算高度。 在這裏你得到了一個週期的依賴關係。 LinearLayout高度爲wrap_content並取決於內部元素的高度之和。 內部元素的高度由父母的高度計算。

此外,你想ImageViewLinearLayout高度的1/10。但實際上好像你想ImageView1/10屏幕的高度,因爲LinearLayout高度可能大於屏幕的高度(並且看起來像這就是你的設備上所獲得的)。

你說,在Android Studio預覽中一切正常。 爲了證明不是一切都OK試試這個:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:fillViewport="true" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     <ImageView 
      android:layout_weight="1" 
      android:adjustViewBounds="true" 
      android:layout_height="0dp" 
      android:layout_gravity="center" 
      android:layout_width="wrap_content" 
      android:id="@+id/image" 
      android:scaleType="fitCenter" 
      android:src="@drawable/bjj2" /> 
     <LinearLayout 
      android:layout_weight="10" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="0dp"> 
      <View 
       android:layout_width="match_parent" 
       android:layout_height="1000dp"/> 

     </LinearLayout> 
    </LinearLayout> 

</ScrollView> 

只是把一些大(比屏幕的高度越大)看你的內內LinearLayout內,你會看到,在預覽圖像比的1/10更大的屏幕的高度。但它是LinearLayout1/10的高度。

如何解決您的問題? 我認爲唯一的方法是計算屏幕的高度1/10和你想擁有和位圖設置爲ImageView圖像的大小調整位圖。 或任何其他方式設置精確的高度爲ImageView

或審查你的佈局,創造不同的東西。

+0

我也希望圖像是的LinearLayout的,而不是屏幕的1/10 - 沒有必要作爲最終的應用程序,但只是作爲一個例子,這是不工作我的手機上。我對你的代碼進行了實驗,只有當內部項目的高度超過屏幕高度(應用程序變成可滾動)時,問題才存在。 這裏我設置視圖200dp的高度和圖像是佈局的1/10: http://i.imgur.com/YHZvFMO.png 這裏查看是400dp,因爲你可以看到圖像填充屏幕的寬度,並且是不佈局的1/10: http://i.imgur.com/HP4cPUH.png http://i.imgur.com/9ybTt3z.png –

+1

我看到的依賴性問題我的代碼。請告訴我,如果我正確理解這一點:「我不應該一起使用LinearLayout上的layout_weight和wrap_content,因爲它們將相互依賴」。但是這種問題只有在scrollview變成可滾動時纔會出現(內部元素超過屏幕高度)。 –

+0

您是否設法解決您的問題? – MadNeox