gpt4 book ai didi

java - Android GridView 可二维滚动

转载 作者:太空宇宙 更新时间:2023-11-04 11:22:02 25 4
gpt4 key购买 nike

我想制作一个由按钮组成的棋盘的游戏,按钮之间没有间距,并且棋盘必须同时在二维上滚动。当我尝试制作嵌套容器时,我可以滚动,例如垂直滚动,但水平滚动会被锁定。

  1. 如何制作可滚动板?
  2. 如何完全消除按钮之间的间距?

最佳答案

要实现这两种滚动行为,您可以实现以下 XML:

现在使用 ScrollView 作为父布局以实现双向滚动。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:scrollbars="vertical">

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="320px" android:layout_height="fill_parent">

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linlay" android:layout_width="320px"
android:layout_height="fill_parent" android:stretchColumns="1"
android:background="#000000"/>

</HorizontalScrollView>

然后要启用水平滚动条,请使用以下命令:

android:scrollbarAlwaysDrawHorizontalTrack="true"

至于按钮上的无间距,您可以通过确保它们与相邻按钮之间没有填充或边距来轻松实现这一点。

只需调整它们的大小即可,以确保它们以所需的设计适合屏幕。

要使用Gridview,您可以执行以下操作:

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<GridView
android:id="@+id/schemeGridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:numColumns="1" >
</GridView>
</LinearLayout>

</HorizontalScrollView>

解决对角滚动问题。我相信您需要处理实际的触摸事件来启动滚动。

试试这个:

@Override
public boolean onTouchEvent(MotionEvent event) {
float curX, curY;

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:
mx = event.getX();
my = event.getY();
break;
case MotionEvent.ACTION_MOVE:
curX = event.getX();
curY = event.getY();
vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
mx = curX;
my = curY;
break;
case MotionEvent.ACTION_UP:
curX = event.getX();
curY = event.getY();
vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
break;
}

return true;
}

在这里找到供引用:Diagonal scrolling

请告诉我这是否有效。

关于java - Android GridView 可二维滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44806658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com