我剛開始的Android,我嘗試下面的問題在這裏張貼之前得到這樣的回答:仍無法按鈕佈局添加Android中
Android - Adding layout at runtime to main layout
Add button to a layout programmatically
Dynamically adding a child to LinearLayout with getting each child's position
而我仍然無法添加按鈕到線性佈局:(
以下是代碼fo R活性,請讓我知道我錯了:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = (LinearLayout) View.inflate(this, R.layout.activity_main, null);
Button btn = new Button(this);
btn.setId(123);
btn.setText("Welcome to WI FI World");
layout.addView(btn);
}
和XML看起來象下面這樣:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
您需要將線性佈局添加到頁面。我很確定那些不是相同的線性佈局。所以你正在添加一個按鈕,但沒有顯示 – Doomsknight