开发者

Align Textviews in different rows using layout_weight parameter

I want to show a list 开发者_运维百科of flights on a listview. And I want to have them all of them align. I have used a LinearLayout, with gravity and padding. But I want to reduce the space for the 4th column, so then I can do bigger the size of the text. I have tried to set the layout_weight of this element to 0 and all of the rest to 1. But it doesnt work. So far I have this

Align Textviews in different rows using layout_weight parameter

Any ideas?


If you are going to use ListView with a list of LinearLayout view's (I would go with a TableRow list put into a TableLayout), then I would suggest predefining the width of each column in percentage of the total space (i.e. how much of the screen would you allow the column to allocate). Something among the lines:

// basically you build your adapter here
LinearLayout.LayoutParams wrapWrapLinearLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int[] columnWidths = new int[]{20, 20, 20, 20, 20};

ArrayList<LinearLayout> tableRows = new ArrayList<LinearLayout>();//this is the adapter
LinearLayout row = new LinearLayout(this);
//add a header
row.setLayoutParams(wrapWrapLinearLayoutParams);
row.setGravity(Gravity.CENTER);
row.setBackgroundColor(headerColor);
row.addView(makeTableRowWithText(headerForCol1, columnWidths[0]));
row.addView(makeTableRowWithText(headerForCol2, columnWidths[1]));
row.addView(makeTableRowWithText(headerForCol3, columnWidths[2]));
row.addView(makeTableRowWithText(headerForCol4, columnWidths[3]));
row.addView(makeTableRowWithText(headerForCol5, columnWidths[4]));
tableRows.add(row);
for(int i = 0; i < numberOfItemsInTheFlightsTable; i++) {
    row = new LinearLayout(this);
    row.setLayoutParams(wrapWrapLinearLayoutParams);
    row.setGravity(Gravity.CENTER);
    row.addView(makeTableRowWithText(col1Text, columnWidths[0]));
    row.addView(makeTableRowWithText(col2Text, columnWidths[1]));
    row.addView(makeTableRowWithText(col3Text, columnWidths[2]));
    row.addView(makeTableRowWithText(col4Text, columnWidths[3]));
    row.addView(makeTableRowWithText(col5Text, columnWidths[4]));
    tableRows.add(row);
}

//util method
private TextView recyclableTextView;

public TextView makeTableRowWithText(String text, int widthInPercentOfScreenWidth) {
    int screenWidth = getResources().getDisplayMetrics().widthPixels;
    recyclableTextView = new TextView(this);
    recyclableTextView.setText(text);
    recyclableTextView.setTextColor(Color.BLACK);
    recyclableTextView.setTextSize(20);
    recyclableTextView.setWidth(widthInPercentOfScreenWidth * screenWidth / 100);
    return recyclableTextView;
}

I mean, the trick is that you predefine the percentage of screen allocated by each column.


Do the following

LinearLayout LL1 containing the first 4 columns

LinearLayout LL2 containing the last column

RelativeLayout RL contains LL1 and LL2

You can now position LL2 to the right of the container

EDITED

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜