【问题标题】:Programmatically set TextView gravity right in a TableRow以编程方式在 TableRow 中设置 TextView 重力
【发布时间】:2015-01-12 19:31:49
【问题描述】:

我知道这已被多次询问,但在我的情况下似乎无法正常工作。我试图让最后一列在以编程方式生成的表中对齐。我知道我需要将 LayoutParams 应用于行和所有内部子项,并且我知道我需要将 Gravity 设置为 TextView,而不是行,但我已经尝试了我能想到的所有排列并且不能似乎让最后一列正确对齐。

这是我的 XML 布局:

<!--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>

然后在我的活动中,我在循环中有以下代码

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

【问题讨论】:

    标签: android android-tablelayout gravity


    【解决方案1】:

    为了让setGravity() 工作,您必须首先在布局中修改 yourTextView 的宽度字段,如下所示:

    android:layout_width="fill_parent"
    

    现在你应该可以调用了:

    tvPeriodHours.setGravity(Gravity.RIGHT)
    

    【讨论】:

    • TextView 不在我的 XML 中,只是在 TableLayout 容器中。当我调用 tvPeriodHours.setLayoutParams(params) 并且 params 被定义为 MATCH_PARENT 时,我以为我正在设置宽度。我的理解不正确吗?
    • 哦,我明白了;如果您指定TableRow.LayoutParams( TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);,它是否有效
    • 当我从 TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT); 更新代码时没有明显变化成为 TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
    • 我刚刚注意到您在拨打setLayoutParams 之前尝试拨打setGravity。尝试在创建 TextView 后立即调用 tvPeriodHours.setLayoutParams(params)
    • 感谢您的帮助顺便说一句:) 我最初是在初始化 TextView 后立即设置 layoutParams,然后将其切换回来,因此代码如下所示。不幸的是,仍然没有变化:( TextView tvPeriodHours = new TextView(getBaseContext()); tvPeriodHours.setLayoutParams(params); tvPeriodHours.setText(currentPeriod); tvPeriodHours.setTextColor(getResources().getColor(R.color.black)); tvPeriodHours.setGravity(Gravity.RIGHT);
    【解决方案2】:

    我认为有两种方法可以做到这一点。

    1. 如果您的 TableLayout 的宽度不是基于其内容(例如,如果使用 android:stretchColumns 设置列宽度),您可以将 TextView 宽度设置为 match_parent,然后使用textView.setGravity(Gravity.END).

    2. 如果您的 TextView 宽度小于 TableLayout 单元格的边界,您可以使用 tableRow.setGravity(Gravity.END) 调用 TableRow 上的重力。

    我有一个 TableLayout,我的 TextViews 填充单元格的宽度而不是高度,所以我正在使用:

    textView.setGravity(Gravity.CENTER);
    tableRow.setGravity(Gravity.CENTER_VERTICAL);
    

    让我的文本位于单元格的中心。

    以防万一它对任何人有帮助,我花了很长时间试图让 TextView 与单元格底部对齐,但似乎没有任何效果,包括 tableRow.setGravity(Gravity.CENTER_VERTICAL),不知道为什么,但我已经放弃了,在中心很好.

    【讨论】:

      猜你喜欢
      • 2013-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 2013-09-26
      • 1970-01-01
      相关资源
      最近更新 更多