更新:我很欣赏使用GridView的建议,我相信这会提供更好的性能.但是,我最初在实现简单的GridView方面的努力导致了可耻的失败.我想弄清楚我做错了什么是另一个话题.为了解决这个实现,我使用OnWindowFocusChanged来查找ANDROID_CONTENT高度,然后除以行数来设置行高.关于这个问题的许多讨论中没有出现的主要教训是OnCreate是设置维度的错误地方.稍后在可以进行精确测量时发生OnWindowFocusChanged.在关于该主题的许多讨论中,并未经常提及此信息.
原文:我正在尝试设置一个包含三列四行的Android布局.
我想拉伸板以垂直和水平填充可用屏幕.我尝试使用weight="1"选项按行和列嵌套LinearLayout ,但只能让它水平拉伸.
在以下配置中,行会拉伸以填充屏幕的宽度,但列设置为"60dp".我也尝试了GridLayout和TableLayout,但未能获得所需的结果.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft = "10dp"
android:paddingRight = "10dp"
android:background="@drawable/bg" >
<LinearLayout
android:id="@+id/player1"
android:layout_width="match_parent"
android:layout_height="@dimen/positionHeight"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:background="@drawable/square" />
<LinearLayout
android:id="@+id/player2"
android:layout_width="match_parent"
android:layout_height="@dimen/positionHeight"
android:layout_alignParentTop="true"
android:orientation="horizontal"
android:background="@drawable/square" />
<LinearLayout
android:id="@+id/row0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/player2"
android:layout_alignParentLeft="true"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/position0"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="@dimen/positionHeight"
android:orientation="horizontal"
android:background="@drawable/square" />
<LinearLayout
android:id="@+id/position1"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="@dimen/positionHeight"
android:orientation="horizontal"
android:background="@drawable/square" />
<LinearLayout
android:id="@+id/position2"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="@dimen/positionHeight"
android:orientation="horizontal"
android:background="@drawable/square" />
</LinearLayout>
<LinearLayout
android:id="@+id/row1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/row0"
android:layout_alignParentLeft="true" …Run Code Online (Sandbox Code Playgroud) 我有一个简单的Racket定义,用于将二进制数相乘.它使用经过充分测试的"addWithCarry"定义,该定义采用三个参数:两个列表和一个进位数,并返回二进制和.二进制数以相反的顺序表示为列表.
我使用调试器逐步完成了测试行,并正确地完成了递归.它每次在适当时缩小y列表时执行multBins,然后按预期执行addWithCarry函数.当它上升到堆栈时,它突然抛出一个异常"应用程序:不是一个过程,期望一个可以应用于参数的过程",参数为'(0 0 0 1 0 1 1),这是最高的值"x"加到了总数中.我知道当您尝试将函数的结果作为带参数的函数应用时,可能会发生此错误,但我在此处未看到此错误.看着调试器,一切看起来都很完美,直到最后.有任何想法吗?
(define (multBins x y)
(cond
((null? y) '() )
((= (first y) 0) ((multBins (cons 0 x) (rest y))))
(#t ((addWithCarry x (multBins (cons 0 x) (rest y)) 0)))))
(test (multBins '(1 0 1 1)'(1 1 0 1))'(1 1 1 1 0 0 0 1))
Run Code Online (Sandbox Code Playgroud)
这是addWithCarry定义:
(define (addWithCarry x y carry)
(cond
((and (null? x)(null? y)) (if (= carry 0) '() '(1)))
((null? x) (addWithCarry '(0) y carry))
((null? y) (addWithCarry x …Run Code Online (Sandbox Code Playgroud)