我正在以编程方式生成 TextViews
的 GridLayout
。我希望 GridLayout 的第二列和第三列 (TextViews
) 具有相同的宽度,并且当 TextViews
中有大量文本时,我希望 TextViews
垂直向下扩展而不是占用更多空间。我试图通过以编程方式将第二个和第三个 TextViews
的列权重设置为 1 来实现此目的。但是,例如,当第二个 TextView
中有大量文本时,第二个和第三个 TextViews
被推到一边。下面是截图:
下面是我的 XML 代码:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rowCount="1"
android:columnCount="3">
<!-- Difference Table -->
<LinearLayout
android:orientation="vertical"
android:id="@+id/difference_table"
android:layout_width="match_parent"
android:layout_column="0"
android:layout_row="0"
android:layout_columnWeight="1"
android:layout_height="wrap_content"></LinearLayout>
</GridLayout>
下面是我的 Kotlin 代码:
val diffLayout = findViewById<LinearLayout>(R.id.difference_table)
var diffTable: GridLayout = GridLayout(this)
// Set the dimensions of the grid
diffTable.layoutParams =
ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
diffTable.columnCount = 3;
diffTable.rowCount = 1;
// Create the three columns and add them to the grid
var lineNumber: TextView = TextView(this)
lineNumber.text = "asdf"
lineNumber.setTextColor(Color.parseColor("#000000"))
lineNumber.gravity = Gravity.LEFT
var currentLine1: TextView = TextView(this)
currentLine1.setTextColor(Color.parseColor("#000000"))
currentLine1.text = "asdfasdfasdfadfasdfadfasdfasdfasdfasdfasdfasdf"
lineNumber.gravity = Gravity.LEFT
// 3. Create the TextView representing the current line in Text 2
var currentLine2: TextView = TextView(this)
currentLine2.setTextColor(Color.parseColor("#000000"))
currentLine2.text = "asdfasdfasdfadfasdfadfasdfasdfasdfasdfasdfasdf"
lineNumber.gravity = Gravity.LEFT
// TODO: ENABLE LAYOUT WEIGHT FOR API < 21
diffTable.addView(lineNumber, GridLayout.LayoutParams(
GridLayout.spec(0, GridLayout.CENTER),
GridLayout.spec(0, GridLayout.CENTER, 0.0f)));
// Add the current line in Text 1 to the table
diffTable.addView(currentLine1, GridLayout.LayoutParams(
GridLayout.spec(0, GridLayout.CENTER),
GridLayout.spec(1, GridLayout.CENTER, 1.0f)));
// Add the current line in Text 2 to the table
diffTable.addView(currentLine2, GridLayout.LayoutParams(
GridLayout.spec(0, GridLayout.CENTER),
GridLayout.spec(2, GridLayout.CENTER, 1.0f)));
最佳答案
1) 扩展 GridLayouts 上的标志应该有问题。
GridLayout
内的两个 View
都应该具有垂直轴和水平轴的权重。尝试为上面的第一个和第二个 View
设置此参数。
final GridLayout.LayoutParams params = new GridLayout.LayoutParams(GridLayout.spec(
GridLayout.UNDEFINED,GridLayout.FILL,1f),
GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f));
2) 作为一种替代且简单的解决方案,您可以将上面的两个 View
替换为单个 LinearLayout
容器,该容器可以自行处理重量。像这样的东西。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="true"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/view.line.1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView]
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/view.line.2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView]
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<LinearLayout>
关于android - 以编程方式生成的 GridLayout 将列推到右侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59121268/