我不明白為什么它不像這樣簡單......
// Add field
val LineLayoutInfo = findViewById<View> (R.id.LineLayoutInfo) as LinearLayout
val Add_Field = findViewById<View>(R.id.buttonAddField) as Button
Add_Field.setOnClickListener{
val tv1 = TextView(this@MainActivity)
tv1.text = "Show Up"val t = TextView(applicationContext)
LineLayoutInfo.addView(tv1)
}
我想做的就是在我當前的LinearLayout中創建一個新的TextView。這一部分代碼是用Kotlin撰寫的,并存盤在OnCreate()中。這可能是因為我需要重繪 活動嗎?
當我按下按鈕時,我得到的輸出看起來是這樣的。
D/HwAppInnerBoostImpl: asyncReportData com.xxx. httpposter,2,1,1, 0 interval=83
I/ViewRootImpl: jank_removeInvalidNode所有節點在jank串列是out的時間
V/AudioManager: playSoundEffect effectType: 0
V/AudioManager: querySoundEffectsEnabled...
D/HwAppInnerBoostImpl: asyncReportData com.xxx. httpposter,2,1,2,0 interval=353
uj5u.com熱心網友回復:
你可以試試這樣的方法
private LinearLayout mLayout;
private Button mButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(s savedInstanceState)。
setContentView(R.layout.main);
mLayout = (LinearLayout) findViewById(R.id.linearLayout);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(onClick())。
TextView textView = new TextView(this);
textView.setText("New text")。
}
private OnClickListener onClick() {
return new OnClickListener() {
public void onClick(View v) {
mLayout.addView(createNewTextView("New Text"/span>))。
}
};
}
private TextView createNewTextView(String text) {
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT) 。
final TextView textView = new TextView(this) 。
textView.setLayoutParams(lparams);
textView.setText("New text: " text);
return textView。
}
uj5u.com熱心網友回復:
在這里,你可以直接創建TextView,并在程式的onCreate()之后的MainActivity上將它的Visibility設定為INVISIBLE,然后在點擊按鈕時將它設定為VISIBLE。你可以無限地這樣做!
。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/311877.html
標籤:
上一篇:如何使用一個lambda的回傳來更新一個可組合的狀態?
下一篇:privatevarb1="a1"和privatevarb2bymutableStateOf("a1")之間有什么區別?
