Android match_parent 未按预期布局工作
Android match_parent not working as expected layout
我有一个自定义视图,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 如何填满所有剩余屏幕 space:
然后我尝试将它嵌入到 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_parent
和 wrap_content
的所有变体,我能想到,但无法让它工作:
edit - 我刚刚发现的另一件有趣的事情是当我删除容器 ScrollView
和 LinearLayout
并且只有 TriggerView
:
<com.roberts.croberts.mantis.TriggerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/triggerView"
android:layout_weight="1"/>
然后设计选项卡说父 class 是 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>
这里有一些问题:
高度定义为 match_parent 的 ScrollView
没有任何意义 - 它应始终设置为 wrap_content。
LinearLayout
高度设置为 match_parent 也是错误的,因为此元素应定义包含 ScrollView
的高度 - 将其更改为 wrap_content。
最后结束 - 您的自定义视图 - 它也应该将高度设置为 wrap_content (或某个固定值)。如果它仍然不能很好地工作,请在此视图中查找问题 - 很难说 - 膨胀布局,或 onMeasure()
方法
您正在尝试使用自定义视图。请post观点class。自定义必须覆盖 onDraw 和 onMeasure 方法并在那里做一些不同的事情。
我有一个自定义视图,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 如何填满所有剩余屏幕 space:
然后我尝试将它嵌入到 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_parent
和 wrap_content
的所有变体,我能想到,但无法让它工作:
edit - 我刚刚发现的另一件有趣的事情是当我删除容器 ScrollView
和 LinearLayout
并且只有 TriggerView
:
<com.roberts.croberts.mantis.TriggerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/triggerView"
android:layout_weight="1"/>
然后设计选项卡说父 class 是 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>
这里有一些问题:
高度定义为 match_parent 的 ScrollView
没有任何意义 - 它应始终设置为 wrap_content。
LinearLayout
高度设置为 match_parent 也是错误的,因为此元素应定义包含 ScrollView
的高度 - 将其更改为 wrap_content。
最后结束 - 您的自定义视图 - 它也应该将高度设置为 wrap_content (或某个固定值)。如果它仍然不能很好地工作,请在此视图中查找问题 - 很难说 - 膨胀布局,或 onMeasure()
方法
您正在尝试使用自定义视图。请post观点class。自定义必须覆盖 onDraw 和 onMeasure 方法并在那里做一些不同的事情。