【问题标题】:Right justified columns in TableLayoutTableLayout 中的右对齐列
【发布时间】:2012-06-05 23:24:39
【问题描述】:

我想发布一张图片来描述这个问题,但显然我在 stackoverflow 方面还没有足够的声誉,所以我将尝试描述我想要完成的事情。

我有一个包含 5 列的 TableLayout。它们由公司名称、工作编号、姓氏、图形垂直分隔线和右箭头按钮组成。我希望分隔线和右箭头按钮与屏幕右侧对齐。我希望第 2 列(工作编号)扩大到足以容纳整个数字而不换行。我希望第 1 列(公司名称)和第 3 列(姓氏)填写表格的其余部分,如果它们太大(没有文字换行)则使用省略号。我的表格是以编程方式构建的。

目前,每当公司名称过长时,分隔线和右箭头就会被推离屏幕右侧。

这是我的 layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical">
<TextView android:id="@+id/searchResultsFoundTextView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textSize="16dp"
          android:text="@string/searchResultsLabel"/>
<ScrollView android:layout_width="fill_parent"
            android:layout_height="fill_parent">
    <TableLayout android:id="@+id/searchResultsTableLayout"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:layout_marginTop="10dp"
                 android:stretchColumns="1"
                 android:shrinkColumns="0,2" />
</ScrollView>
</LinearLayout>

这是我构建表格的代码:

TableLayout table = (TableLayout)findViewById(R.id.searchResultsTableLayout);
for (JobBase job : jobList) {
  TableRow row = new TableRow(this);
  row.setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
  row.setPadding(5, 10, 5, 20);
  table.addView(row);

  TextView customerNameTextView = new TextView(this);
  customerNameTextView.setText(job.getCustomerName());
  customerNameTextView.setEllipsize(TextUtils.TruncateAt.END);
  customerNameTextView.setTextSize(FONT_SIZE);
  customerNameTextView.setPadding(10, 5, 10, 0);
  row.addView(customerNameTextView);

  TextView jobNumberTextView = new TextView(this);
  jobNumberTextView.setText(job.getJobNumber());
  jobNumberTextView.setTextSize(FONT_SIZE);
  jobNumberTextView.setPadding(10, 5, 10, 0);
  row.addView(jobNumberTextView);

  TextView crewLeaderTextView = new TextView(this);
  crewLeaderTextView.setText(job.getCrewLeader().getLastName());
  crewLeaderTextView.setEllipsize(TextUtils.TruncateAt.END);
  crewLeaderTextView.setTextSize(FONT_SIZE);
  crewLeaderTextView.setPadding(10, 5, 10, 0);
  row.addView(crewLeaderTextView);

  ImageView dividerImageView = new ImageView(this);
  dividerImageView.setImageDrawable(getResources().getDrawable(R.drawable.btn_med_hl_div_white));
  dividerImageView.setPadding(10, 10, 10, 0);
  row.addView(dividerImageView);

  Button rightArrowButton = new Button(this);
  rightArrowButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.right_arrow_button));
  rightArrowButton.setPadding(10, 0, 10, 0);
  rightArrowButton.setOnClickListener(new RowSelectedAction(row));
  row.addView(rightArrowButton);

  row.setOnClickListener(new RowSelectedAction(row));
}

任何帮助将不胜感激。

【问题讨论】:

    标签: android tablelayout


    【解决方案1】:

    我可以通过 LayoutParams 向第一列添加权重来解决此问题:

    TextView customerNameTextView = new TextView(this);
    customerNameTextView.setText(job.getCustomerName());
    // --- Added this statement
    customerNameTextView.setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f));
    // --- 
    customerNameTextView.setEllipsize(TextUtils.TruncateAt.END);
    customerNameTextView.setTextSize(FONT_SIZE);
    customerNameTextView.setPadding(10, 5, 10, 0);
    row.addView(customerNameTextView);
    

    第一列现在扩展和收缩,并且分隔符和按钮始终右对齐。有时客户名称是椭圆形的,有时名称是换行的,但这对我来说并不重要,只要列大小正确即可。

    【讨论】:

      猜你喜欢
      • 2012-08-24
      • 1970-01-01
      • 1970-01-01
      • 2014-10-01
      • 2012-08-24
      • 2012-08-21
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      相关资源
      最近更新 更多