【问题标题】:Android : LinearLayout with weight inside a ScrollViewAndroid:在 ScrollView 内具有权重的 LinearLayout
【发布时间】:2016-11-29 01:36:43
【问题描述】:

我的 ScrollView 遇到了这个令人沮丧的问题,这是我的层次结构:

ScrollView (Vertical)
    LinearLayout (Vertical)
        ImageView weight= 0.5
        Whatever weight= 0.1
        Whatever weight= 0.2
        Whatever weight= 0.2

如果我删除 ScrollView(并让 LinearLayout 作为主要项目),这适用于任何 Image :图像占据屏幕大小的 50%,而其他视图占据其余部分。

但是,当 ScrollView 在这里时,如果我的图像太高,“权重”参数将被完全忽略:图像会尽其所能适应屏幕宽度,然后显然太高并且需要超过屏幕的 50%。编辑:实际上,所有“重量”属性似乎都被忽略了:

没有滚动视图:

使用滚动视图:

我希望线性布局完全适合而无需滚动。那可能吗 ?我尝试更改一些图像选项(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,因为我使用的是 PullToRefresh ScrollView

【问题讨论】:

  • 只是出于好奇,如果你不想滚动,为什么需要滚动视图?
  • 基本上这个^。滚动视图没有维度,它可以是无限的。 50% 你期望你的 imageview 是什么?如果是线性布局,它知道。 ScrollView 根据需要占用尽可能多的空间。
  • 这似乎不对。看看developer.android.com/reference/android/support/percent/…这可能是你需要的。
  • 如果你想修复它,在 ScrollView 中对布局进行分组是没有意义的。
  • 我需要这个 ScrollView,因为我正在使用 PullToRefresh 库和 ScrollView。当用户尚未滚动时,我希望我的图像为屏幕大小的 50%。

标签: java android xml scrollview android-linearlayout


【解决方案1】:

也许为时已晚,但您的问题可以通过将android:fillViewport="true" 添加到您的ScrollView 来解决:

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

【讨论】:

    【解决方案2】:

    我通过添加一个父线性布局(和一个主要的 relativeLayout)来解决它:

        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的高度设置为屏幕高度

    【讨论】:

    • 了解当前行为会很棒。我同意了,这个解决方案奏效了!
    • 代码(几乎)总是有效;声明性的东西....少得多:-)我也走这条路。谢谢。
    【解决方案3】:

    试试这个方法。

    <?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% 的高度要求。
    • 真的……wrap content 怎么会尊重百分比?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多