我有两个自定义视图,我想垂直布局。第一个视图是一种游戏板,它是一个正方形。正方形易于缩放,可以适应几乎所有可用空间。
第二个视图是其子类[TableLayout][1]
,包含数个Button
s。行数/列数在运行时确定。
为了便于说明,我将称为第一个视图板和第二个表。
我希望将板显示在屏幕顶部,并且表格应位于其正下方。表应该占据所需的垂直空间(这基本上取决于行数)。木板应将其正方形边缘尺寸设置为可用宽度和可用高度的最小值,即缩小。
我试图用一个[RelativeLayout][2]
这样的实现:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/default_padding"
>
<some.package.Table
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/default_padding"
/>
<some.package.Board
android:id="@+id/board"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_above="@id/table"
/>
</RelativeLayout>
通过这种方法我曾希望表将首先测量和剩余的可用屏幕高度将是最大的(即传递AT_MOST
)高度规格的onMeasure
中板。这个假设似乎是错误的。
当我使用这种布局时,电路板似乎已调整为零尺寸,即未渲染。
当我像这样扭转局面时:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/default_padding"
>
<some.package.Board
android:id="@+id/board"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<some.package.Table
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/default_padding"
android:layout_below="@id/board"
/>
</RelativeLayout>
该电路板占用了太多的垂直空间和桌子被在底部切断
在这两种情况下,板都将显示宽度作为AT_MOST
宽度规格,将显示高度(减去状态栏和操作栏高度)作为AT_MOST
高度规格。因此,我的onMeasure
实现将两者的最小值作为正方形,这并没有为表格视图留出足够的空间。
有人可以提请我如何处理吗?是否可能需要添加嵌套布局?还是使用布局权重?-我不这样做,因为权重是我理解中的相对大小。
谢谢和亲切的问候
加贝回答后编辑:
在尝试Gabe的建议后,我在board和tableonMeasure
方法中记录了一些数据:
table: wM=EXACTLY minW=0 ws=736 hM=EXACTLY minH=0 hs=1006
board: wM=AT_MOST minW=0 ws=736 hM=AT_MOST minH=0 hs=1006
board: wM=EXACTLY minW=0 ws=736 hM=EXACTLY minH=0 hs=1006
table: wM=EXACTLY minW=0 ws=736 hM=AT_MOST minH=0 hs=0
I/Choreographer﹕ Skipped 206 frames! The application may be doing too much work on its main thread.
D/dalvikvm﹕ GC_FOR_ALLOC freed 305K, 8% free 4880K/5252K, paused 103ms, total 105ms
table: wM=EXACTLY minW=0 ws=736 hM=EXACTLY minH=0 hs=1006
board: wM=AT_MOST minW=0 ws=736 hM=AT_MOST minH=0 hs=1006
board: wM=EXACTLY minW=0 ws=736 hM=EXACTLY minH=0 hs=1006
table: wM=EXACTLY minW=0 ws=736 hM=AT_MOST minH=0 hs=0
wM是的宽度模式MeasureSpec
,minW是getSuggestedMinimumWidth()
,ws是的宽度尺寸MeasureSpec
。同样适用于hM,minH和hs。
我没有onMeasure
在表中覆盖(是的,我有,但是它只是记录数据并调用super.onMeasure()
)。board.onMeasure()
上面描述了实现的行为。
我还是很困惑
在kha的答案后进行编辑:
卡的建议奏效了。有效的XML布局是
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/default_padding"
>
<some.package.Table
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/default_padding"
android:layout_alignParentBottom="true"
/>
<some.package.Board
android:id="@+id/board"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_above="@id/table"
/>
</RelativeLayout>
现在,日志记录输出为:
board: wM=AT_MOST minW=0 ws=736 hM=AT_MOST minH=0 hs=1006
table: wM=EXACTLY minW=0 ws=736 hM=EXACTLY minH=0 hs=1006
table: wM=EXACTLY minW=0 ws=736 hM=AT_MOST minH=0 hs=1006
board: wM=EXACTLY minW=0 ws=736 hM=EXACTLY minH=0 hs=702
I/Choreographer﹕ Skipped 118 frames! The application may be doing too much work on its main thread.
board: wM=AT_MOST minW=0 ws=736 hM=AT_MOST minH=0 hs=1006
table: wM=EXACTLY minW=0 ws=736 hM=EXACTLY minH=0 hs=1006
table: wM=EXACTLY minW=0 ws=736 hM=AT_MOST minH=0 hs=1006
board: wM=EXACTLY minW=0 ws=736 hM=EXACTLY minH=0 hs=702
试试这个:
移动并更改
android:layout_below="@id/board"
到
android:layout_above="@id/table"
在您的其他布局上。
结合设置android:alignParentBottom=true
表格布局来执行此操作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句