我膨脹的佈局activity_main
,但是當我點擊一個按鈕什麼都沒有發生,textview沒有更新。 下面是代碼:膨脹的視圖不更新
private int clicks = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.RIGHT | Gravity.TOP;
View myview = li.inflate(R.layout.activity_main, null);
wm.addView(myview, params);
Button button = (Button)findViewById(R.id.button);
TextView textview = (TextView)findViewById(R.id.textview);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clicks +=1;
textview.setText("Clicks: " + clicks);
}
});
}
它看起來像點擊在第一activity_main
(setContentView(R.layout.activity_main);
)按鈕更新TextView的,而不是一個在充氣視圖(一個是在前面)。
爲什麼會發生這種情況,以及如何使點擊按鈕更新充氣視圖中的textview?
另外,有沒有辦法只有充氣的看法?如果我刪除setContentView(R.layout.activity_main);
應用程序崩潰。
您爲什麼要再次爲activity_main充氣? –