问题
I know this has been asked several times over, but can't seem to get it to work correctly in my situation. I am trying to get the last column to align right in a table that is generated programatically. I know I need to apply the LayoutParams to the row and all the inner children, and I know that I need to set the Gravity to the TextView, not the row, but I have tried all the permutations I can think of and can't seem to get the last column to align right.
Here is my XML layout:
<!--Open Hours-->
<LinearLayout
android:id="@+id/llOpenHourDetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/llWebsiteDetail"
android:paddingBottom="10dp"
android:visibility="gone"
android:weightSum="4">
<TextView
android:id="@+id/tvOpenHourDetailIcon"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/fa_clock"
android:textColor="@color/cp_blue" />
<TableLayout
android:id="@+id/tlOpenHoursDetail"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3" />
</LinearLayout>
And then in my activity I have the following code in a loop
String currentPeriod = formattedOpen.format(open.getTime()) + " - " +
formattedClose.format(close.getTime());
TableRow.LayoutParams params = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT );
TableRow tableRow = new TableRow(getBaseContext());
tableRow.setLayoutParams(params);
TextView tvDayOfWeek = new TextView(getBaseContext());
tvDayOfWeek.setText(open.getDisplayName(
Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault()));
tvDayOfWeek.setTextColor(getResources().getColor(R.color.black));
tvDayOfWeek.setLayoutParams(params);
tableRow.addView(tvDayOfWeek);
TextView tvPeriodHours = new TextView(getBaseContext());
tvPeriodHours.setText(currentPeriod);
tvPeriodHours.setTextColor(getResources().getColor(R.color.black));
tvPeriodHours.setGravity(Gravity.RIGHT);
tvPeriodHours.setLayoutParams(params);
tableRow.addView(tvPeriodHours);
tlOpenHoursDetail.addView(tableRow);
回答1:
In order for setGravity()
to work you must first modify the width field of yourTextView
as follows in your layout:
android:layout_width="fill_parent"
Now you should be able to call:
tvPeriodHours.setGravity(Gravity.RIGHT)
回答2:
I think there are two ways of doing this.
If your TableLayout has a width not based on it's contents (such as if the columns widths are set using
android:stretchColumns
) you can set the TextView width tomatch_parent
and then assign the gravity on the TextView usingtextView.setGravity(Gravity.END)
.If your TextView width is smaller than the bounds of the TableLayout cell you can call the gravity on the TableRow instead using
tableRow.setGravity(Gravity.END)
.
I have a TableLayout where my TextViews fill the width of the cell but not the height so I'm using:
textView.setGravity(Gravity.CENTER);
tableRow.setGravity(Gravity.CENTER_VERTICAL);
To get my text in the very centre of the cell.
In case it helps anyone, I have spent ages trying to get the TextView aligned to the bottom of the cell but nothing seems to work, including tableRow.setGravity(Gravity.CENTER_VERTICAL)
, no idea why but I have given up and in the centre is fine.
来源:https://stackoverflow.com/questions/27909357/programmatically-set-textview-gravity-right-in-a-tablerow