Programmatically set TextView gravity right in a TableRow

混江龙づ霸主 提交于 2019-12-11 02:33:32

问题


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.

  1. 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 to match_parent and then assign the gravity on the TextView using textView.setGravity(Gravity.END).

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!