【问题标题】:I am creating the textView Dynamically我正在动态创建 textView
【发布时间】:2017-08-26 12:22:04
【问题描述】:

当我动态创建文本视图时,我想在运行代码完成后将 hello 文本设置在中心。

TextView textView;
private TextView createNewTextView(String text) {
     textView = new TextView(this)
     textView.setText(" hello ");
     textView.setGravity(Gravity.CENTER_HORIZONTAL);
}

【问题讨论】:

  • 这段代码不能正常工作

标签: android dynamic textview


【解决方案1】:

试试下面的代码:

LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,              LinearLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(LinearLayout.CENTER_IN_PARENT);
textView.setLayoutParams(lp);

【讨论】:

  • 如何使用addRule
【解决方案2】:

使用:
textView.setTextAlignment(Gravity.CENTER_HORIZONTAL);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-03
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多