我正在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>
有人能告訴我什麼,我做錯了。
我也希望圖像是的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 –
我看到的依賴性問題我的代碼。請告訴我,如果我正確理解這一點:「我不應該一起使用LinearLayout上的layout_weight和wrap_content,因爲它們將相互依賴」。但是這種問題只有在scrollview變成可滾動時纔會出現(內部元素超過屏幕高度)。 –
您是否設法解決您的問題? – MadNeox