【问题标题】:Can't center TextView dynamically in TableRow无法在 TableRow 中动态居中 TextView
【发布时间】:2011-08-07 16:45:15
【问题描述】:

我已经动态地制作了一个带有 ImageButton 和 TextView 的 TableRow。 我使用TableRow.setGravity(Gravity.CENTER_VERTICAL);在我的 TableRow 上设置了重力

然后,在我的 TextView 上,我正在使用此代码

TextView tv = new TextView(this);
tv.setText(functie[beschrijving]);
tv.setWidth(300);
//tv.setHeight(60);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setPadding(15, 0, 10, 0);
tr.addView(tv);

当我这样做时,我得到了这个结果。它不是完全居中,它看起来像中心正下方的文本。有谁知道如何将文本完全放在中心?

【问题讨论】:

    标签: android textview center tablerow


    【解决方案1】:

    嗨 我认为您应该使用 Parent Layout 的以下属性 android:layout_centerVertical="true"

    【讨论】:

    • 您是指 TableLayout 本身吗?因为我也在 Java 代码中这样做。
    【解决方案2】:

    也许这段代码对你有帮助。

    android.widget.TableRow.LayoutParams params = new android.widget.TableRow.LayoutParams();
    params.gravity = Gravity.CENTER_VERTICAL;
    tr.addView(tv, params);
    

    但我不确定。

    【讨论】:

    • 它没有帮助,它删除了橙色边框顶部像素的几行。
    猜你喜欢
    • 2013-04-19
    • 1970-01-01
    • 2011-09-16
    • 2012-02-02
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多