我正在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