当前位置: 首页 > 面试题库 >

如何获得Android TableLayout填充屏幕?

沈树
2023-03-14
问题内容

我正在与Android的糟糕布局系统作斗争。我正在尝试用一张桌子填满屏幕(很简单吗?),但是很难做到。

我得到它可以以某种方式在XML中工作:

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent">
<TableRow android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1">
<Button android:text="A" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
<Button android:text="B" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
</TableRow>
<TableRow android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1">
<Button android:text="C" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
<Button android:text="D" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
</TableRow>

但是我无法使其在Java中工作。我已经尝试了LayoutParams的一百万种组合,但是没有任何效果。这是我得到的最佳结果,它只能填充屏幕的宽度,而不是高度:

    table = new TableLayout(this);
    // Java. You suck.
    TableLayout.LayoutParams lp = new TableLayout.LayoutParams(
                                    ViewGroup.LayoutParams.FILL_PARENT,
                                    ViewGroup.LayoutParams.FILL_PARENT);
    table.setLayoutParams(lp); // This line has no effect! WHYYYY?!
    table.setStretchAllColumns(true);
    for (int r = 0; r < 2; ++r)
    {
        TableRow row = new TableRow(this);
        for (int c = 0; c < 2; ++c)
        {
            Button btn = new Button(this);
            btn.setText("A");
            row.addView(btn);
        }
        table.addView(row);
    }

显然,Android文档没有帮助。有人有主意吗?


问题答案:

终于弄清楚了如何做到这一点。放弃TableLayout并只LinearLayout在垂直的s中使用了水平的s。关键是设置重量。如果您指定FILL_PARENT但使用默认权重,则无法使用:

LinearLayout buttonsView = new LinearLayout(this);
buttonsView.setOrientation(LinearLayout.VERTICAL);
for (int r = 0; r < 6; ++r)
{
    LinearLayout row = new LinearLayout(this);
    row.setOrientation(LinearLayout.HORIZONTAL);
    for (int c = 0; c < 4; ++c)
    {
        Button btn = new Button(this);
        btn.setText("A");
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        lp.weight = 1.0f;
        row.addView(btn, lp);
    }
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
    lp.weight = 1.0f;
    buttonsView.addView(row, lp);
}

ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
setContentView(buttonsView, lp);


 类似资料:
  • 我有一个要填充信息的对象。我从许多不同的服务中检索信息。我做了一个助手类,它有一个公共方法,然后有许多私有方法来调用服务。我写的东西很好,但我不确定这是否是正确的方法。 您可能想知道为什么我需要一个包含所有这些信息的对象。我需要它全部在一个对象中,因为我从这个java对象创建了一个json对象并将其传递给javascript层。 我的方法有什么问题?我是否应该遵循一种编程范式来做这样的事情? 例子

  • 问题内容: 有谁知道你将如何获得屏幕宽度的Java?我读了一些关于工具箱方法的内容,但是我不确定那是什么。 谢谢,安德鲁 问题答案:

  • 问题内容: 我需要用ArrayList填充JComboBox。有什么办法吗? 问题答案: 使用ArrayList类的方法并将其传递给 有关更多信息,请参见JavaDoc和教程。

  • 问题内容: 如何获得以像素为单位的屏幕分辨率(宽x高)? 我正在使用JFrame和Java swing方法。 问题答案: 你可以使用该方法获取屏幕尺寸。 在多显示器配置中,你应该使用以下命令: 如果要在DPI中获得屏幕分辨率,则必须使用上的getScreenResolution()方法Toolkit。

  • 我试图填充LSTM迷你批处理的张量序列,序列中的每个时间步都包含一个子张量列表(在单个时间步中表示多个特征)。 例如,序列1有3个时间步,每个时间步有2个特征。下面的一个例子是: 序列1=[[1,2],[2,2],[3,3],[3,2],[3,2]] 序列2=[[4,2],[5,1],[4,4]] 序列3=[[6,9]] 我运行pytorch的pad_sequence函数(这也适用于pack_se

  • 问题内容: 我目前正在使用Page_Load中的以下代码创建和读取DataTable 我想知道如何转换此代码,以便它从SQL查询中读取?我正在尝试下面的代码,但不确定如何连接它们,以便页面加载中的数据表使用下面的SQL命令填充。 我被困在: 我希望成为 问题答案: 您需要修改方法,并在其中添加“实验”代码,然后返回。