我想在运行代码完成后在中心设置hello文本,因为我正在动态创建文本视图。
TextView textView;
private TextView createNewTextView(String text) {
textView = new TextView(this)
textView.setText(" hello ");
textView.setGravity(Gravity.CENTER_HORIZONTAL);
}
答案 0 :(得分:0)
请尝试以下代码:
LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(LinearLayout.CENTER_IN_PARENT);
textView.setLayoutParams(lp);
答案 1 :(得分:0)
使用:
textView.setTextAlignment(Gravity.CENTER_HORIZONTAL);