XML 布局权重在 android API23 上的 ScrollView 中不起作用



我正在AndroidStudio中做一个Android项目,其中ScrollView包含一个垂直的LinearLayout,在该LinearLayout中有一个ImageView和另一个LinearLayout。我正在尝试将ImageView设置为比LinearLayout小10倍,结果在预览窗口中显示正确,但在我的手机上显示不正确(带有API3的Xperia Z23)。

这是我在预览窗口中看到的,也是我想要实现的

但是在我的手机上,图像填充了屏幕的宽度,图像高度与原始图像成正比(几乎填满了屏幕的一半,根据权重,它应该是什么)

该应用程序的工作方式就像在带有API25的Google Pixel 7.1.1上一样。

以下是代码的一部分:

<?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

有人可以告诉我我做错了什么。

人们应该了解如何计算尺寸。 让我们一一看。

1)ScrollViewfill_parent/fill_parent(实际上可以将其更改为match_parent/match_parent因为据我所知fill_parent已被弃用,但在这种情况下没关系)。 因此,显示ScrollView适合整个屏幕(宽度和高度)。

2)ScrollView内部LinearLayoutmatch_parent/wrap_content。这是正确的。应该将wrap_content用于内部的视图ScrollView. 原因是 -ScrollView用于包装通常未知高度的视图(如列表)。在这里想象match_parent高度 -LinearLayout的高度将根据滚动视图的高度(即整个屏幕高度)计算,LinearLayout高度可以大于屏幕高度。只是不合逻辑。但到目前为止一切都很好。冘。

3)LinearLayout内的物品。您希望它们1/109/10父布局的高度。此外,还可以将内部项目的height设置为0,以显示您希望从weights计算高度。 在这里,你得到了一个依赖关系的循环LinearLayout高度是wrap_content的,取决于内部元素的高度总和。 内部元素的高度是根据父LinearLayout高度计算的。

此外,您希望ImageViewLinearLayout高度的 1/10。但实际上,似乎您希望ImageView屏幕高度1/10,因为LinearLayout高度可以大于屏幕的高度(看起来这就是您在设备上获得的高度)。

你说,在Android Studio的预览中一切都很好。 要证明并非一切都正常,请尝试以下操作:

<?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。但这1/10LinearLayout的身高。

如何解决您的问题? 我认为唯一的方法是计算屏幕高度的1/10并使用您想要的图像调整位图的大小,并将该位图设置为ImageView. 或任何其他将确切高度设置为ImageView的方法。

或者查看您的布局并创建一些不同的东西。

我们可以通过避免使用显示指标layout_weight来在运行时设置图像视图的高度。

例如

DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
yourImageView.getLayoutParams().height = height / 5;
yourImageView.getLayoutParams().width = width / 4;

最新更新