android 中通过代码创建控件

时间:2021-06-25 23:06:42
package bvb.de.openadbwireless.circle;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast; import java.util.ArrayList;
import java.util.List; public class AddViewByJava extends Activity { private Context context = this;
private RelativeLayout rl_root;
private TextView tv; @TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 最外层的控件 RelativeLayout
rl_root = new RelativeLayout(context); // 添加一个TextView
tv = new TextView(context);
RelativeLayout.LayoutParams tvLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
// 设置 margin
tvLayoutParams.leftMargin = 50;
// 设置 padding
tv.setPadding(10, 10, 10, 10);
tv.setText("textView");
tv.setLayoutParams(tvLayoutParams);
rl_root.addView(tv); //添加一个按钮
Button btn = new Button(context);
btn.setText("button");
// 设置规则 : 在TextView 的下方
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW, tv.getId());
btn.setLayoutParams(layoutParams);
rl_root.addView(btn);
btn.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
tv.setText(getChild());
}
}); // 在Button 下面添加一个LinearLayout,里面水平放4个Button
LinearLayout ll = new LinearLayout(context);
RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams1.addRule(RelativeLayout.BELOW, btn.getId());
ll.setLayoutParams(layoutParams1);
ll.setBackgroundColor(Color.GREEN);
ll.setPadding(10, 50, 0, 0);
rl_root.addView(ll); ll.setOrientation(LinearLayout.HORIZONTAL);
int btnCount = 4;
for (int i = 0; i < btnCount; i++) {
final Button button = new Button(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT));
params.weight = 1;
button.setText("button" + i);
ll.addView(button, params);
button.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
Toast.makeText(context, button.getText(), Toast.LENGTH_SHORT).show();
}
});
} ListView lv = new ListView(context);
RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams2.addRule(RelativeLayout.BELOW, ll.getId());
lv.setLayoutParams(layoutParams2);
rl_root.addView(lv);
List<String> datas = new ArrayList<String>();
for (int i = 0; i < 20; i++) {
datas.add("name" + i);
}
lv.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, android.R.id.text1, datas)); setContentView(rl_root);
} public String getChild() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < rl_root.getChildCount(); i++) {
sb.append(rl_root.getChildAt(i).toString()).append("\n");
}
return sb.toString();
} }