我是一名 Android 新手,试图利用我的 VB 经验(8 年前)设计一个 UI。我正在尝试创建一个棋盘,在 VB 中,它是一种形式,我可以根据需要在多行中连续添加多个可调整大小的面板小部件。由于这些是面板,我可以在上面添加一个小图像(硬币)(以面板为背景),或者甚至添加另一个小面板,其颜色我可以设置为可见和不可见来代表硬币。我知道描述 VB UI 很糟糕,但 VB 旨在简化表单设计,它确实做到了,这是我能想到的用于 UI 的唯一语言。
我注意到 android SDK 几乎没有足够的小部件供我使用。我能想到的最好的方法是使用包含多行的 TableLayout。我不明白的是我用什么来表示正方形?是否有类似于 VB 面板小部件的东西?我不想使用图像,因为我希望电路板自动调整到屏幕尺寸。
有人可以帮我一些提示吗?
最佳答案
您可以使用水平 LinearLayout 在 XML 中定义布局,并用 8 个 ImageView(或任何其他可以显示图像/颜色的容器)填充它,然后在垂直 LinearLayout 中复制它 7 次,类似于:
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<ImageView android:id="@+id/square_1"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
<!-- Set image or background here --> />
<!-- Repeat the ImageView 7 times and change the id for every ImageView
you create -->
</LinearLayout>
<!-- Reapeat the LinearLayout 7 times too -->
</LinearLayout>
这将创建 8 行,每行有 8 个正方形,并且所有正方形都将具有相同的大小,这要归功于 weight 属性,这表明它们都应该获得相等的空间。
要使用 Java 代码更改图像/背景,您必须使用:
ImageView square_1 = (ImageView)findViewById(R.id.square_1);
square_1.setBackground(Color.yellow);
在此处阅读 Android SDK 引用中的更多信息:
- ImageView :http://developer.android.com/reference/android/widget/ImageView.html
- 线性布局:http://developer.android.com/reference/android/widget/LinearLayout.html
希望这对您有所帮助!
关于android - 为 Android 应用绘制棋盘(跳棋),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4870823/