【问题标题】: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);
【解决方案2】:
使用:
textView.setTextAlignment(Gravity.CENTER_HORIZONTAL);