通缉:TableLayout样的ListViewTableLayout、ListView

2023-09-04 11:24:00 作者:蹦擦蹦擦蹦擦擦

我的问题可能是,怎样才能创建一个单行的水平LinearLayout中有两个单线非包装TextViews,其中左侧的TextView始终占据可用屏幕宽度的1/3,右TextView中始终占据2 / 3的可用屏幕宽度,和文本标签截断与...当他们太长时间才能显示? (如果这个问题是不够清楚,读者 - 这是你的 - 已经在脑海中的解决方案,他们愿意分享,进一步阅读的问题,问题的描述可能是不必要的)

My question might be, "How does one create a single-line horizontal LinearLayout with two single-line non-wrapping TextViews, where the left TextView always occupies 1/3 of the available screen width, the right TextView always occupies 2/3 of the available screen width, and the text labels truncate with ... when they are too long to display?" (If that question is clear enough and the reader -- that's you -- already has in mind a solution they are willing to share, further reading of the problem and question description may be unnecessary.)

填充有四个这样的LinearLayouts一个ListView然后将是这个样子:

A ListView populated with four such LinearLayouts would then look something like this:

medium length text ---- short text
short text         ---- text that is too loooooooooooooo...
loooooooooooooo... ---- short text
loooooooooooooo... ---- text that is too loooooooooooooo...

在本实体模型,的第一行中,左而右侧的文本标签是足够短,完全显示,第二行中,左侧的标签很短,足以显示完全,而右标签的文本太长,由此截断,第三行在开始第一排的右侧的文本垂直排列,在视觉上营造一个列,如果这是在一个TableLayout正确标签的文字,,左侧标签的文本是时间过长,被截断,以便与在第二和第一行的左边文本垂直对齐,并且右标签的文本开始垂直与所述第二和第一行的右侧的文本,的对准和第四行中,左侧标签的文本太长,因此同样被显示在第三行左文本,右标签的文本太长,因此同样被显示在第二个的右边的文本行。

In this mock-up, in the first row, the left and right text labels were short enough to display completely, in the second row, the left label was short enough to display completely, while the text of the right label was too long and was thus truncated, and the start of the text of the right label aligned vertically with the right text of the first row, visually creating a column as if this were in a TableLayout, in the third row, the text of the left label was too long and was truncated so as to vertically align with the left text of the second and first rows, and the text of the right label started vertically aligned with the right text of the second and first rows, and in the fourth row, the text of the left label was too long and thus was displayed similarly to the left text of the third row, and the text of the right label was too long and thus was displayed similarly to the right text of the second row.

从上计算器和其他地方不同的岗位,我推测,在使用TableLayout与ArrayAdapter有些可能的,也有缺点,它可能不是永远正确的方法服用。

From various posts on stackoverflow and other places, I gather that while using a TableLayout with an ArrayAdapter is somewhat possible, there are downsides, and it is probably not ever the right approach to take.

我当然试过的布局参数多种组合,但什么也还没有到非常接近目标。我想弄清楚,将工作在许多不同的屏幕尺寸的解决方案,因此指定特定的像素宽度可能就不能很好地工作。

I've certainly tried many combinations of layout parameters, but nothing has yet come very close to the goal. I'm trying to figure out a solution that will work on many different screen sizes, so specifying specific pixel widths probably wouldn't work very well.

也许下面这个简单的code例子(带屏幕截图)是一个很好的起点解决方案,而只是需要一些小的修改。

Perhaps the following simple code example (with screenshot) is a good starting point for the solution, and just needs some small modifications.

(该计算器code格式被吓坏了这个code,所以请原谅任何时髦的格式。)

(The stackoverflow code formatter was freaking out about this code. So, please forgive any funky formatting.)

public class TableLayoutLikeListView extends ListActivity
{
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);

    List<Datum> data = new ArrayList<Datum>();
    data.add(new Datum("short", "short"));
    data.add(new Datum("short", "loooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnng"));
    data.add(new Datum("loooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnng", "short"));
    data.add(new Datum("loooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnng",
        "loooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnng"));
    setListAdapter(new MyAdapter(this, R.layout.row, data));
  }
}

class Datum
{
  String left, right;

  Datum(String left, String right)
  {
    this.left = left;
    this.right = right;
  }
}

class MyAdapter extends ArrayAdapter<Datum>
{
  List<Datum> data;
  int textViewResourceId;

  MyAdapter(Context context, int textViewResourceId, List<Datum> data)
  {
    super(context, textViewResourceId, data);
    this.data = data;
    this.textViewResourceId = textViewResourceId;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent)
  {
    if (convertView == null)
    {
      LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      convertView = inflater.inflate(textViewResourceId, null);
    }
    Datum datum = data.get(position);
    if (datum != null)
    {
      TextView leftView = (TextView) convertView.findViewById(R.id.left);
      TextView rightView = (TextView) convertView.findViewById(R.id.right);
      if (leftView != null)
      {
        leftView.setText(datum.left);
      }
      if (rightView != null)
      {
        rightView.setText(datum.right);
      }
    }
    return convertView;
  }
}

list.xml: 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ListView
        android:id="@+id/android:list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="3">
    <TextView
        android:id="@+id/left"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="----" />
    <TextView
        android:id="@+id/right"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:singleLine="true" />
</LinearLayout>

(请注意,虽然TextView中确实有ellipsize属性,它似乎没有必要改变,如设置单行真截短和ellipsizes标签了。我可能是错的这一点。)

(Note that while TextView does have the ellipsize attribute, it doesn't appear necessary to alter, as setting singleLine to true truncates and ellipsizes the labels already. I may be wrong about this.)

以下是此code在行动截图:

Following is a screenshot of this code in action:

任何意见,当然是AP preciated。

Any advice is of course appreciated.

推荐答案

您可以使用 TableLayout 并使其行为类似于的ListView (至少在视觉上)。 Here 的是我的回答。

You can use TableLayout and make it to behave like ListView (at least visually). Here is my answer.