这没有xml。我的问题是为什么按钮不显示在屏幕上?
我添加了一个带有 setContentView 的布局并添加了按钮。为什么不显示?
package com.my.layoutexample;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout mainLayout = new LinearLayout(null);
mainLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
mainLayout.setOrientation(LinearLayout.VERTICAL);
setContentView(mainLayout);
Button button = new Button(null);
button.setText("Send");
button.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
mainLayout.addView(button);
}
}