android - 以编程方式生成的 GridLayout 将列推到右侧

标签 android kotlin textview android-xml android-gridlayout

我正在以编程方式生成 TextViewsGridLayout。我希望 GridLayout 的第二列和第三列 (TextViews) 具有相同的宽度,并且当 TextViews 中有大量文本时,我希望 TextViews 垂直向下扩展而不是占用更多空间。我试图通过以编程方式将第二个和第三个 TextViews 的列权重设置为 1 来实现此目的。但是,例如,当第二个 TextView 中有大量文本时,第二个和第三个 TextViews 被推到一边。下面是截图:

enter image description here

但是,这就是我希望我的应用程序的样子: enter image description here

下面是我的 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/

相关文章:

kotlin - 如何使用 Ktor 和 Kotlin 下载带有进度指示器的大文件?

android - onCreate 之外的 TextView.setText 不起作用

java - 如何使用带 `java`的with `kotlin-compiler.jar`命令编译Kotlin源代码?

android - BottomSheetDialogFragment 未显示

Android Studio 无法解析符号textView

android - 如何对 TextView 中的每个单词使用 onTouchListeners?

Android 应用程序旋转时意外停止

android - 通知中不显示按钮

android - 权限拒绝 : writing to settings requires android. permission.WRITE_SETTINGS

android - 如何检查 Google Play Services API 的可用性? (特别是 GCM API)