gpt4 book ai didi

java - 如何在 TableRow 中创建两列,使用最大的一列相同 “width”

转载 作者:行者123 更新时间:2023-12-02 02:35:20 25 4
gpt4 key购买 nike

我有一个简单的 TableRow 布局,每个布局有两个 TextView 列:

<TableRow>
<TextView
android:text="@string/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="@string/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TableRow>

在“onCreate”中,以编程方式设置每列上的文本:

TextView col1 = findViewById(R.id.tv1);
TextView col2 = findViewById(R.id.tv2);
col1.setText("LARGE TEXT COLUMN");
col2.setText("SHORT");

下面的代码就像一个魅力,可以以编程方式设置 col2 宽度,适用于我喜欢的任何尺寸:

ViewGroup.LayoutParams params = col2.getLayoutParams();
params.width = XXX;
col2.setLayoutParams(params);

这个想法是读取“col1”宽度,将其分配给“col2”,但是,无论我将以下代码放在“onCreate”或“onResume”、“col1.getWidth()”或“col1”中的位置。 getMeasuredWidth()”始终为 cero“0”:

ViewGroup.LayoutParams params = col2.getLayoutParams();
params.width = col1.getWidth();
col2.setLayoutParams(params);

..或

ViewGroup.LayoutParams params = col2.getLayoutParams();
params.width = col1.getMeasuredWidth();
col2.setLayoutParams(params);

有什么想法吗?

最佳答案

一个不优雅的解决方案是添加一个虚拟的不可见表格行,其中包含 col2 = col1 内容,因此它将具有相同的一面,但同样,它并不优雅。

<TableRow
android:visibility="invisible">
<TextView
android:text="@string/tv1_dummy"
android:layout_width="wrap_content"
android:layout_height="1dp">
<TextView
android:text="@string/tv2_dummy"
android:layout_width="wrap_content"
android:layout_height="1dp">
</TableRow>
<TableRow>
<TextView
android:text="@string/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="@string/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TableRow>

然后...

((TextView)findViewById(R.id.tv1_dummy)).setText("LARGE TEXT COLUMN");
((TextView)findViewById(R.id.tv2_dummy)).setText("LARGE TEXT COLUMN");
((TextView)findViewById(R.id.tv1)).setText("LARGE TEXT COLUMN");
((TextView)findViewById(R.id.tv2)).setText("SHORT");

或者真正的虚拟没有优雅的xml代码,没有任何编程代码:

之前:

        <TableRow android:gravity="center">
<TextView
android:text="@string/m_avaItems"
android:textStyle="bold"
style="@style/Text_Shop" />
<TextView
android:text="@string/w_tokens"
android:textStyle="bold"
style="@style/Text_Shop" />
<TextView
android:text= "@string/w_usd"
android:textStyle="bold"
style="@style/Text_Shop" />
</TableRow>

enter image description here

之后:

        <TableRow
android:visibility="invisible"
android:gravity="center">
<View
android:layout_width="wrap_content"
android:layout_height="0px"
android:layout_span="2"/>
<TextView
android:text= "@string/w_tokens"
android:layout_height="0px"
android:textStyle="bold"
style="@style/Text_Shop" />
</TableRow>
<TableRow android:gravity="center">
<TextView
android:text="@string/m_avaItems"
android:textStyle="bold"
style="@style/Text_Shop" />
<TextView
android:text="@string/w_tokens"
android:textStyle="bold"
style="@style/Text_Shop" />
<TextView
android:text= "@string/w_usd"
android:textStyle="bold"
style="@style/Text_Shop" />
</TableRow>

enter image description here

如果有人给我发送了一个简单而漂亮的编程代码评估器,而不是没有优雅的虚拟 XML 代码,请给我一个优雅的拥抱。祝你有美好的一天

关于java - 如何在 TableRow 中创建两列,使用最大的一列相同 “width”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57207332/

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