2012-05-29 61 views
1

我想發佈一張圖片來描述這個問題,但顯然我還沒有足夠的信譽與stackoverflow,所以我會試圖描述我想要完成的。TableLayout中右對齊的列

我有一個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)); 
} 

任何幫助將非常感激。

回答

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); 

第一列現在膨脹和收縮,並分頻器和按鈕總是右對齊。有時候客戶名稱是橢圓化的,其他時候名稱是包裝的,但只要色譜柱的大小正確,對我來說就沒有關係。