【问题标题】:Android match_parent not working as expected layoutAndroid match_parent 未按预期布局工作
【发布时间】:2016-07-27 20:24:32
【问题描述】:

我有一个自定义视图,TriggerView。当我在视图中覆盖onDraw 时,我绘制的图形有时会溢出视图的可视范围之外。所以我想将视图嵌入到 ScrollView 中,以便图形可以滚动。

这是我的代码,没有任何ScrollViews

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.roberts.croberts.mantis.ShotTriggerFragment"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:clipChildren="false">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        ...
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageContainer"
        android:orientation="horizontal"
        android:layout_weight="1">

        <com.roberts.croberts.mantis.TriggerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/triggerView" />

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">
        ...

    </LinearLayout>
</LinearLayout>

在此屏幕截图中查看 triggerView 如何填充所有剩余的屏幕空间:

然后我尝试将其嵌入到 ScrollView 中:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageContainer"
        android:orientation="horizontal">

        <com.roberts.croberts.mantis.TriggerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/triggerView" />

    </LinearLayout>
</ScrollView>

然后 TriggerView 高度缩小到 0。我已经尝试了 match_parentwrap_content 的所有变体,我能想到,但无法让它工作:

编辑 - 我刚刚发现的另一件有趣的事情是当我删除容器 ScrollViewLinearLayout 并只拥有 TriggerView 时:

<com.roberts.croberts.mantis.TriggerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/triggerView"
        android:layout_weight="1"/>

然后设计标签说父类是ScrollView?:

【问题讨论】:

  • 没有显示您在 onDraw 中覆盖的内容,并且可能与测量相关的方法,您可能只会得到“盲拍”答案......您的代码 &lt;LinearLayout android:layout_height="0dp" 和您的自定义视图中也有,所以很明显它具有相同的高度......

标签: android android-linearlayout android-custom-view android-scrollview


【解决方案1】:
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageContainer"
        android:orientation="horizontal">

        <com.roberts.croberts.mantis.TriggerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/triggerView" />

    </LinearLayout>
</ScrollView>

这里的一些问题: ScrollView 高度定义为 match_parent 没有意义 - 它始终应设置为 wrap_content。 LinearLayout 高度设置为 match_parent 也是错误的,因为此元素应定义包含 ScrollView 的高度 - 将其更改为 wrap_content。 最后结束 - 您的自定义视图 - 它还应该将高度设置为 wrap_content (或某个固定值)。如果它仍然不能正常工作,请在此视图中查找问题 - 很难说 - 膨胀的布局,或 onMeasure() 方法

【讨论】:

  • 还有android:fillViewport attr,可能有用。 CHECK
  • 我需要的最大技巧就是重写 onMeasure() 方法
【解决方案2】:

您正在尝试使用自定义视图。请发布视图类。自定义必须覆盖 onDraw 和 onMeasure 方法并在那里做一些不同的事情。

【讨论】:

    猜你喜欢
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-28
    • 2013-08-20
    相关资源
    最近更新 更多