【问题标题】:Setting height equal to width with width being attributted by weight设置高度等于宽度,宽度由重量决定
【发布时间】:2014-03-30 18:39:18
【问题描述】:

我正在为 android 制作一个基于 2048 的游戏,我添加的功能之一是可以有一个从 4x4 到 8x8 的网格,但是我在分配图块的大小时遇到​​了问题。我有一个包含其他 LinearLayouts 的 LinearLayout,每行一个(网格高度),并且在每一行 TextViews 内为该行中的每个图块。每个 textview 的权重为 1,包含它们的行的 weightSum 等于宽度(以瓷砖为单位),宽度设置为 match_parent,然后在将 textview 添加到布局以具有正方形后,我将高度设置为等于宽度。我正在通过代码执行此操作,为每个图块填充预定义的 textview 并将其添加到行中,然后将行添加到“板”视图中。但它不起作用。

这是在 onCreate 方法中为视图分配宽度、高度和重量的相关代码:

        board = (LinearLayout) findViewById(R.id.board);
        board.setWeightSum(1.0f * height);

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        for (int i = 0; i < height; i++) {

            LinearLayout row = (LinearLayout) inflater.inflate(R.layout.row_ll, null);
            lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
            row.setId(1000 + i);
            row.setWeightSum(1.0f * width);

            for (int j = 0; j < width; j++) {

                tv = (TextView) inflater.inflate(R.layout.text_view, null);

                tv.setId(nextId());

                row.addView(tv, lp);

            }

            board.addView(row);

            for (int j = 0; j < width; j++) {

                tv = (TextView) findViewById((i * width) + j + 1);
                tv.setHeight(tv.getMeasuredWidth());
                System.out.println("h:" + tv.getHeight() + "; w:" + tv.getWidth());

            }

        }

text_view:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_view"
android:layout_width="0dip"
android:layout_height="0dip"
android:background="@drawable/bg_tv"
android:gravity="center"
android:text="@string/zero"
android:textColor="@color/text"
android:textSize="16sp"
android:layout_weight="1" >

</TextView>

row_ll

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row_ll"
android:layout_width="match_parent"
android:layout_height="64dip"
android:background="@drawable/bg_tv"
android:orientation="horizontal"
android:weightSum="4">

</LinearLayout>

其余代码可以在https://github.com/lhamaware/2048找到

【问题讨论】:

  • GridLayout 可能是你最好的朋友。
  • 由于向后兼容性,我不打算使用它,但现在你提到它,我不得不增加最小版本,因为其他原因,我最终可能会使用 GridLayouts,但我想知道是什么这段代码有问题,因为如果我需要在其他地方使用它,我就能做到。
  • GridLayout 是 v7 appcompat 库的一部分 http://developer.android.com/tools/support-library/features.html

标签: java android android-layout


【解决方案1】:

您需要在测量阶段设置高度。为此,请创建一个扩展 TextView 的自定义视图,并覆盖 onMeasure

public void onMeasure(int widthSpec, int heightSpec) {
  super.onMeasure(widthSpec, heightSpec);
  int width = getMeasuredWidth();
  setMeasuredDimension(width, width);
}

查看我的博文了解更多信息:http://blog.sqisland.com/2012/01/android-square-view.html

【讨论】:

    猜你喜欢
    • 2020-06-11
    • 2016-03-08
    • 2023-03-23
    • 2021-10-08
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 2020-09-25
    相关资源
    最近更新 更多