gpt4 book ai didi

android - TableLayout 中的右对齐列

转载 作者:行者123 更新时间:2023-11-30 04:11:26 29 4
gpt4 key购买 nike

我想发布一张图片来描述这个问题,但显然我在 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));
}

如有任何帮助,我们将不胜感激。

最佳答案

我能够通过 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);

第一列现在展开和收缩,分隔线和按钮始终右对齐。有时客户名称会被省略,有时会被换行,但只要列大小正确,这对我来说并不重要。

关于android - TableLayout 中的右对齐列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10802230/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com