以编程方式生成的 GridLayout 将列推到右侧
Programmatically generated GridLayout has column being pushed to the right
我正在以编程方式生成 TextViews
的 GridLayout
。我希望 GridLayout
的第二和第三列 (TextViews
) 具有相同的宽度,并且当 TextViews
中有很多文本时,我希望 TextViews
垂直向下扩展而不是占用更多 space。我试图通过以编程方式将第二个和第三个 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>
我在你之前的代码中稍微修改了一下,添加了width = 0
属性,可能会达到你想要的显示效果
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// setContentView(R.layout.activity_main)
// val diffLayout = findViewById<LinearLayout>(R.id.difference_table)
// val diffTable = findViewById<GridLayout>(R.id.grid_layout);
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.CENTER
// 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.CENTER
var layoutParams = GridLayout.LayoutParams()
layoutParams.rowSpec = GridLayout.spec(0, GridLayout.FILL)
layoutParams.columnSpec = GridLayout.spec(0, GridLayout.FILL)
// TODO: ENABLE LAYOUT WEIGHT FOR API < 21
diffTable.addView(lineNumber, layoutParams)
var layoutParams1 = GridLayout.LayoutParams()
layoutParams1.rowSpec = GridLayout.spec(0, GridLayout.FILL)
layoutParams1.columnSpec = GridLayout.spec(1, GridLayout.FILL, 1f)
layoutParams1.width = 0
// Add the current line in Text 1 to the table
diffTable.addView(currentLine1, layoutParams1)
// Add the current line in Text 2 to the tableLEFT
var layoutParams2 = GridLayout.LayoutParams()
layoutParams2.rowSpec = GridLayout.spec(0, GridLayout.FILL)
layoutParams2.columnSpec = GridLayout.spec(2, GridLayout.FILL, 1f)
layoutParams2.width = 0
diffTable.addView(currentLine2, layoutParams2)
addContentView(diffTable, ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT))
}
我的建议是动态设置视图的列宽。最后,它会将每个视图与所需的 space.
数量对齐
var diffTable: GridLayout = GridLayout(this)
diffTable.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
diffTable.setColumnCount(3);
diffTable.setRowCount(1);
TextView lineNumber;
for (int i = 0; facilities != null && i < facilities.size(); i++) {
lineNumber = new TextView(getContext());
lineNumber.setText(facilities.get(i));
diffTable.addView(lineNumber, i);
lineNumber.setCompoundDrawablesWithIntrinsicBounds(rightIc, 0, 0, 0);
GridLayout.LayoutParams param =new GridLayout.LayoutParams();
param.height = LayoutParams.WRAP_CONTENT;
param.width = LayoutParams.WRAP_CONTENT;
param.rightMargin = 5;
param.topMargin = 5;
param.setGravity(Gravity.CENTER);
param.columnSpec = GridLayout.spec(c);
param.rowSpec = GridLayout.spec(r);
lineNumber.setLayoutParams (param);
}
其中c
代表'column index',r
代表GridLayout的行索引。
注意:如果您想插入参数,以上代码可能需要稍作改动。
我正在以编程方式生成 TextViews
的 GridLayout
。我希望 GridLayout
的第二和第三列 (TextViews
) 具有相同的宽度,并且当 TextViews
中有很多文本时,我希望 TextViews
垂直向下扩展而不是占用更多 space。我试图通过以编程方式将第二个和第三个 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>
我在你之前的代码中稍微修改了一下,添加了width = 0
属性,可能会达到你想要的显示效果
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// setContentView(R.layout.activity_main)
// val diffLayout = findViewById<LinearLayout>(R.id.difference_table)
// val diffTable = findViewById<GridLayout>(R.id.grid_layout);
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.CENTER
// 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.CENTER
var layoutParams = GridLayout.LayoutParams()
layoutParams.rowSpec = GridLayout.spec(0, GridLayout.FILL)
layoutParams.columnSpec = GridLayout.spec(0, GridLayout.FILL)
// TODO: ENABLE LAYOUT WEIGHT FOR API < 21
diffTable.addView(lineNumber, layoutParams)
var layoutParams1 = GridLayout.LayoutParams()
layoutParams1.rowSpec = GridLayout.spec(0, GridLayout.FILL)
layoutParams1.columnSpec = GridLayout.spec(1, GridLayout.FILL, 1f)
layoutParams1.width = 0
// Add the current line in Text 1 to the table
diffTable.addView(currentLine1, layoutParams1)
// Add the current line in Text 2 to the tableLEFT
var layoutParams2 = GridLayout.LayoutParams()
layoutParams2.rowSpec = GridLayout.spec(0, GridLayout.FILL)
layoutParams2.columnSpec = GridLayout.spec(2, GridLayout.FILL, 1f)
layoutParams2.width = 0
diffTable.addView(currentLine2, layoutParams2)
addContentView(diffTable, ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT))
}
我的建议是动态设置视图的列宽。最后,它会将每个视图与所需的 space.
数量对齐var diffTable: GridLayout = GridLayout(this)
diffTable.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
diffTable.setColumnCount(3);
diffTable.setRowCount(1);
TextView lineNumber;
for (int i = 0; facilities != null && i < facilities.size(); i++) {
lineNumber = new TextView(getContext());
lineNumber.setText(facilities.get(i));
diffTable.addView(lineNumber, i);
lineNumber.setCompoundDrawablesWithIntrinsicBounds(rightIc, 0, 0, 0);
GridLayout.LayoutParams param =new GridLayout.LayoutParams();
param.height = LayoutParams.WRAP_CONTENT;
param.width = LayoutParams.WRAP_CONTENT;
param.rightMargin = 5;
param.topMargin = 5;
param.setGravity(Gravity.CENTER);
param.columnSpec = GridLayout.spec(c);
param.rowSpec = GridLayout.spec(r);
lineNumber.setLayoutParams (param);
}
其中c
代表'column index',r
代表GridLayout的行索引。
注意:如果您想插入参数,以上代码可能需要稍作改动。