package com.demo;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private String str;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//创建布局管理器
LinearLayout linearLayout=new LinearLayout(this);
//设置activtity
super.setContentView(linearLayout);
//垂直布局
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
TextView textView=new TextView(this);
Button button=new Button(this);
button.setText("这是用Java代码创建的按钮");
button.setLayoutParams(new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
));
linearLayout.addView(textView);
linearLayout.addView(button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "创建成功了!", Toast.LENGTH_SHORT).show();
}
});
}
}
安卓学习之--使用Java代码创建Layout--实例
最新推荐文章于 2024-08-13 18:31:04 发布