Android: 在ScrollView中使用带有权重的LinearLayout

3
我正在使用ScrollView时遇到了麻烦,以下是我的层级结构:
ScrollView (Vertical)
    LinearLayout (Vertical)
        ImageView weight= 0.5
        Whatever weight= 0.1
        Whatever weight= 0.2
        Whatever weight= 0.2

如果我移除ScrollView(让LinearLayout作为主要项),这将正常工作并适用于任何图像:该图像占据屏幕大小的50%,其他视图填满剩余空间。

然而,当ScrollView存在时,如果我的图片太高,“weight”参数将完全被忽略:图像会尽其所能适应屏幕宽度,然后显然会过高并且占据超过屏幕的50%。编辑:实际上,所有“weight”属性似乎都被忽略了:

没有ScrollView: enter image description here

有ScrollView: enter image description here

我希望LinearLayout完美适合,无需滚动。 是否可能? 我已经尝试更改某些图像选项(scaleType,adjustViewBounds),但我没有成功。

以下是整个xml代码:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:adjustViewBounds="true"
        android:src="@drawable/testing" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.2"
        android:text="I'M A TEST" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.1"
        android:text="I'M A TEST" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.2"
        android:text="I'M A TEST" />

</LinearLayout>
</ScrollView>

注意:我需要使用ScrollView,因为我正在使用下拉刷新ScrollView。

3
只是出于好奇,如果你不想滚动,为什么需要滚动视图? - wanpanman
1
基本上这个滚动视图没有固定的尺寸,可以无限扩展。当你指定一个ImageView在ScrollView中应该占据多少空间时,如果它是线性布局,那么它会自动计算出来。而ScrollView则会占据它所需要的所有空间。 - Vucko
1
这似乎不太对。请查看https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html,它可能是你需要的。 - tynn
如果您想要一个固定的布局,那么在ScrollView中对其进行分组是没有意义的。 - Phantômaxx
1
我需要这个ScrollView,因为我正在使用ScrollView来使用PullToRefresh库。 当用户还没有滚动时,我希望我的图像占屏幕大小的50%。 - Tom
那么你将不会拥有一个固定的布局,正如你已经自己发现的那样。 - Phantômaxx
3个回答

9
也许现在有点晚了,但是您的问题可以通过在 ScrollView 中添加 android:fillViewport="true" 来解决:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

3
我通过添加一个父级线性布局(和一个主要的相对布局)来解决它:
    ScrollView (Vertical)
        LineaLayout
            LinearLayout (Vertical)
                ImageView weight= 0.5
                Whatever weight= 0.1
                Whatever weight= 0.2
                Whatever weight= 0.2

通过使用childLinearLayout.getLayoutParams().height在编程方式下将子LinearLayout的高度设置为屏幕的高度。


1
了解当前的行为将是很好的。我同意,这个解决方案可行! - Максим Петлюк
代码(几乎)总是有效的;声明性的东西……往往不那么有效 :-) 我也选择这条路。谢谢。 - Daniele Muscetta

-1

试试这个方法。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:adjustViewBounds="true"
        android:padding="50dp"
        android:src="@drawable/ic_launcher" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:padding="20dp"
        android:text="I&apos;M A TEST" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:padding="20dp"
        android:text="I&apos;M A TEST" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:padding="10dp"
        android:text="I&apos;M A TEST" />
</LinearLayout>
</ScrollView>

但这样不会满足50%,20%,20%,10%的高度要求。 - Phantômaxx
真的吗... wrap content 怎么可能尊重百分比呢? - Phantômaxx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接