1)扩展GridLayouts上的标志应该有问题。
View
中的两个GridLayout
都应具有垂直和水平轴的权重。尝试为上面的第一个View
和第二个final GridLayout.LayoutParams params = new GridLayout.LayoutParams(GridLayout.spec(
GridLayout.UNDEFINED,GridLayout.FILL,1f),GridLayout.spec(GridLayout.UNDEFINED,1f));
设置此参数。
View
2)作为一种替代且简单的解决方案,您可以用单个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>
容器,这可以自己处理重量。这样的事情。
z-index
,
我在您之前的代码中做了一些修改,并添加了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,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,1f)
layoutParams2.width = 0
diffTable.addView(currentLine2,layoutParams2)
addContentView(diffTable,ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT))
}
,
我的建议是动态设置视图的列宽。最后,它将使每个视图与所需的空间对齐。
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);
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
代表“列索引”,r
代表GridLayout的行索引。
注意:如果您要插入参数,则上面的代码可能需要做一些更改。
本文链接:https://www.f2er.com/3000763.html