2017-04-01 66 views
-4

我想在運行代碼完成後將中間的hello文本設置爲中心,因爲我正在動態創建文本視圖。我正在動態創建textView

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

此代碼不能正常工作 – Abhinav

回答

0

試試下面的代碼:

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

如何利用addRule – Abhinav

0

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

+0

不沃金在動態分配texrtview – Abhinav