仍然可以';t在Android中添加按钮到布局

仍然可以';t在Android中添加按钮到布局,android,android-layout,Android,Android Layout,我刚刚启动了android,在将其发布到这里之前,我尝试了以下问题来获得答案: 我仍然无法在线性布局中添加按钮:( 以下是活动代码,请告诉我哪里错了: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout layout =

我刚刚启动了android,在将其发布到这里之前,我尝试了以下问题来获得答案:

我仍然无法在线性布局中添加按钮:(

以下是活动代码,请告诉我哪里错了:

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>

为您的线性布局提供一个id

然后在代码中

LinearLayout layout = (LinearLayout)findViewById(R.id.given_id);

其余部分保持不变,应该可以使用。

为线性布局提供一个id

然后在代码中

LinearLayout layout = (LinearLayout)findViewById(R.id.given_id);

保持其余部分不变,应该可以使用。

在XML中获取LinearLayout的Id,在jave代码中使用XML中的LinearLayout的Id

<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"
    android:id="@+id/linear" >

</LinearLayout>

在XML中获取LinearLayout的Id,在jave代码中使用XML中LinearLayout的Id

<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"
    android:id="@+id/linear" >

</LinearLayout>

尝试为布局分配id,然后将按钮添加到布局中

我很确定这两个布局是不一样的,所以你实际上是在给一个从未显示过的布局添加一个按钮

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LinearLayout layout = (LinearLayout) findViewById(R.id.lnr_main);

    Button btn = new Button(this);
    btn.setId(123);
    btn.setText("Welcome to WI FI World");
    layout.addView(btn);
}
为布局分配了一个id

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/lnr_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

</LinearLayout>

尝试为布局分配id,然后将按钮添加到布局中

我很确定这两个布局是不一样的,所以你实际上是在给一个从未显示过的布局添加一个按钮

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LinearLayout layout = (LinearLayout) findViewById(R.id.lnr_main);

    Button btn = new Button(this);
    btn.setId(123);
    btn.setText("Welcome to WI FI World");
    layout.addView(btn);
}
为布局分配了一个id

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/lnr_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

</LinearLayout>


您需要将线性布局添加到页面中。我非常确定这些线性布局不相同。因此,您需要将线性布局添加到页面中。我非常确定这些线性布局不相同。因此,您要将按钮添加到未显示的按钮中