当前位置: 首页 > 知识库问答 >
问题:

如何在纸牌游戏中使用网格布局

魏冷勋
2023-03-14

我正在开发一个内存卡游戏,它有十二张卡,三行由四张卡组成。为此,布局由三个线性布局组成,每个布局包含四个文本视图。

我如何用另一种方式使用网格布局?

这是我创建卡片行的布局xml:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:weightSum="4">

    <ImageView
        android:id="@+id/iv_a"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:scaleType="centerInside"
        android:src="@drawable/card" />

    <ImageView
        android:id="@+id/iv_b"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:scaleType="centerInside"
        android:src="@drawable/card" />

    <ImageView
        android:id="@+id/iv_c"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:scaleType="centerInside"
        android:src="@drawable/card" />

    <ImageView
        android:id="@+id/iv_d"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:scaleType="centerInside"
        android:src="@drawable/card" />

</LinearLayout>

共有2个答案

左丘宜然
2023-03-14
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="4"
    android:rowCount="3" >

    <ImageView
        android:id="@+id/iv_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerInside"
        android:src="@drawable/card" />

    <ImageView
        android:id="@+id/iv_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerInside"
        android:src="@drawable/card" />

    .
    .
    .

</ GridLayout>

阅读:https://medium.com/google-developer-experts/android-grid-layout-1faf0df8d6f2

何烨华
2023-03-14

在这种情况下,您可以使用GridView。我把我非常简化的代码放在这里,所以您可以使用它们并根据需要更改它们。这个GridView只有12个图像视图。

  1. 为GridView创建适配器类:

MyGridViewAdapter。JAVA

public class MyGridViewAdapter extends BaseAdapter {

    private Context context;
    private List<Integer> drawables;


    MyGridViewAdapter(Context context) {
        this.context = context;
    }

    void setDrawables(List<Integer> drawables)
    {
        this.drawables = drawables;
    }

    public int getCount() {
        return drawables.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create new ImageViews for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {

        // main layout of each item of gridView:
        RelativeLayout relativeLayout=new RelativeLayout(context);
        relativeLayout.setLayoutParams(new GridView.LayoutParams((int)dpToPx(context, 100),
                (int)dpToPx(context, 100)));

        // images:
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(R.drawable.ic_launcher_background);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT));
        relativeLayout.addView(imageView);

        return relativeLayout;
    }

    private float dpToPx(Context context, float dp) {
        return dp * context.getResources().getDisplayMetrics().density;
    }
}

活动测试。xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TestActivity"
    android:orientation="vertical">

    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/gridView"
        android:numColumns="auto_fit"
        android:gravity="center"
        android:columnWidth="100dp"
        android:stretchMode="columnWidth"
        android:choiceMode="singleChoice"
        android:drawSelectorOnTop="true"
        android:focusable="true"
        android:clickable="true"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:layout_margin="10dp"/>

</LinearLayout>

TestActivity.java

public class TestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        // list of 12 images:
        List<Integer> drawables=new ArrayList<>();
        for(int i=0; i<12; i++)
        {
            drawables.add(R.drawable.ic_launcher_background);
        }

        // gridView adapter:
        MyGridViewAdapter adapter = new MyGridViewAdapter(this);
        adapter.setDrawables(drawables);

        // gridView:
        GridView gridView = findViewById(R.id.gridView);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                // what happen when you click on each item
            }
        });
    }
}

结果是:

祝你好运

 类似资料:
  • 对于家庭作业,我得到了下面的代码和空白的方法。我一直在努力,但我仍然不明白mutator setComparer或Comparator comparer在这个程序中是如何工作的。我在网上研究了如何使用比较器,但想法仍然不清楚。有人能给我一些指导吗?。 我 必须初始化比较器比较器吗?如果是这样,我该怎么做? 上面的私人int比较(扑克牌一,扑克牌二)是什么意思?(this.comparer=null

  • 我试图用Java实现纸牌游戏“Skat” 规则: 有32张卡片。每张牌在游戏中只存在一次。 颜色有:梅花、黑桃、红心、钻石 价值观是:王牌,国王,王后,杰克,十,九,八,七 那些牌被洗牌了。有三名球员 第一轮:每位玩家有3张牌。然后将两张卡片放入Skat。 第二轮:每位玩家再获赠4张牌。 第三轮:每位玩家再获得3张牌。 期望输出:每个玩家的牌都被展示(每张牌都应该存在一次) 我的产量:钻石七(32

  • 我尝试在Java实施纸牌游戏“滑冰” 规则: 有32张牌。每张牌在游戏中只存在一次。 颜色是:梅花、黑桃、红心、钻石 这些值是:王牌、国王、王后、杰克、十、九、八、七 那些牌被洗牌了。有三个队员 第一回合:每位玩家获赠3张牌。然后将2张牌放入滑板中。 第二回合:每名玩家再获4张牌。 第三回合:每名玩家再获3张牌。 想要的输出:露出的每个玩家的牌(每张牌都要存在一次) 我的输出:钻石七(32次) 我

  • 我在做一个匹配的纸牌游戏,但我不明白如何实际做到这一点。我希望有人能帮我解释一下我能做些什么来解决这个问题。 以下是规格: 在一个对的纸牌游戏中,目标是翻出对匹配的纸牌。 https://en.wikipedia.org/wiki/contension_(游戏)以下是我们考虑的配对变化的规则。 在游戏开始时,有n张牌面朝下,其中n是4的倍数。每种卡片有4张,卡片上标有字母a、b、....例如,如果

  • 我正在尝试创建一个MVC模式的回合制卡牌游戏。我对正确的设计有些怀疑。 我创建了一个由这些类组成的模型: 卡(使用套装) 套牌(40张牌,抽取牌法,剩余牌数) 表 套装(enum) 玩家(手牌,人名,手牌数,收藏牌我想) 游戏模型(引用了Deck、Table,并在构造函数中包含两个玩家) 我将在用户按下“新游戏”按钮时创建两个Player对象和GameModel(计算机和Player)。这样做对吗

  • 本文向大家介绍java实现24点纸牌游戏,包括了java实现24点纸牌游戏的使用技巧和注意事项,需要的朋友参考一下 本文题目为大家分享了java实现24点纸牌游戏的具体代码,供大家参考,具体内容如下 题目 24点游戏是经典的纸牌益智游戏。 常见游戏规则: 从扑克中每次取出4张牌。使用加减乘除,第一个能得出24者为赢。(其中,J代表11,Q代表12,K代表13,A代表1),按照要求编程解决24点游戏