【问题标题】:How to align items precisely in a table layout?如何在表格布局中精确对齐项目?
【发布时间】:2018-07-05 07:26:16
【问题描述】:

我无法在表格布局中对齐项目。

当最后一项超过一行时,您可以看到下一个文本转到下一行,但它不能与上面的文本完全一致。

这是xml:

<TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="right">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Notes"
                    android:layout_column="1"/>

                <TextView
                    android:id="@+id/txt_notes"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_column="2"
                    android:singleLine="false"
                    android:layout_weight="1"
                    android:maxLines="4"/>

            </TableRow>

我已经尝试在新的文本视图中设置冒号,但没有。

所以我尝试在 java 中添加冒号和 setText, 这是java类:

orderListViewHolder.txtDate.setText(": "+itemOrderList.get(position).getDate());
        orderListViewHolder.txtOrderNumber.setText(": "+itemOrderList.get(position).getOrderNumber());
        orderListViewHolder.txtCustomerName.setText(": "+itemOrderList.get(position).getCustomerName());
        orderListViewHolder.txtProductStatus.setText(": "+itemOrderList.get(position).getProductStatus());
        orderListViewHolder.txtNotes.setText(": "+itemOrderList.get(position).getNotes());

如何精确对齐这些项目?

【问题讨论】:

    标签: android android-tablelayout


    【解决方案1】:

    我尝试了您的布局,如果您在两个 TextView 之间插入带有文本 : 的 TextView,它工作正常:

            <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Notes"
            android:layout_column="1"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=":"
            android:layout_column="2"/>
    
        <TextView
            android:id="@+id/txt_notes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_column="3"
            android:singleLine="false"
            android:layout_weight="1"
            android:maxLines="4"/>
    
    </TableRow>
    

    【讨论】:

      【解决方案2】:

      根据您的问题,您应该使用任何具有水平方向的基本线性布局。

      请看一下

        <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal">
      
                  <TextView
                      android:layout_width="0dp"
                      android:layout_height="wrap_content"
                      android:padding="5dp"
                      android:text="Notes :"
                      android:textSize="12sp"
                      android:layout_weight="1"
                      android:layout_margin="5dp"
                      android:gravity="left"
                      android:textColor="@android:color/black"/>
      
                  <TextView
                      android:layout_width="0dp"
                      android:layout_height="wrap_content"
                      android:padding="2dp"
                      tools:text="Motor Bekas Honda Supra X"
                      android:layout_weight="1"
                      android:textSize="12sp"
                      android:layout_margin="5dp"
                      android:gravity="left"
                      android:textColor="@android:color/black"/>
      
              </LinearLayout>
      

      但是,如果您想使用表格布局,请尝试以下示例

      <TableLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <TableRow
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
      
      
              <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_margin="5dp"
                  android:gravity="left"
                  android:padding="5dp"
                  android:text="Notes :"
                  android:textColor="@android:color/black"
                  android:textSize="12sp" />
      
              <TextView
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_margin="5dp"
                  android:layout_weight="1"
                  android:gravity="left"
                  android:padding="2dp"
                  android:textColor="@android:color/black"
                  android:textSize="12sp"
                  tools:text="Motor Bekas Honda Supra X ,Motor Bekas Honda Supra X,Motor Bekas Honda Supra X" />
      
      
          </TableRow>
      
          <TableRow
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
      
      
              <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_margin="5dp"
                  android:gravity="left"
                  android:padding="5dp"
                  android:text="Any xyz :"
                  android:textColor="@android:color/black"
                  android:textSize="12sp" />
      
              <TextView
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_margin="5dp"
                  android:layout_weight="1"
                  android:gravity="left"
                  android:padding="2dp"
                  android:textColor="@android:color/black"
                  android:textSize="12sp"
                  tools:text="Motor Bekas Honda Supra" />
      
      
          </TableRow>
      
      
      </TableLayout>
      

      截图:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-13
        • 2012-12-09
        • 2012-07-24
        • 1970-01-01
        • 2012-12-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-05
        相关资源
        最近更新 更多