如何设置以编程方式创建的按钮的属性?例如:在我下面的例子中,我想创建一个" android:layout_weight =" 1""和" on_click =" startsearch""属于我的" Place"按钮。
List<DB_Verlauf_Contact> placeList = db.getAllDBPlaces();
LinearLayout layout = (LinearLayout) findViewById(R.id.verlauf_liste);
for (DB_Verlauf_Contact cn : placeList) {
String log = "Id: "+cn.getID()+" ,Place ID: " + cn.getPlace_id() + " ,Name: " + cn.getName()+ " ,Longitude: " + cn.getLongitude()+ " ,Latitude: " + cn.getLatitude();
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Button Place = new Button(this);
Place.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Place.setText(cn.getName());
Place.setId(cn.getID());
row.addView(Place);
Button Delete = new Button(this);
Delete.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Delete.setText("Löschen");
Delete.setId(cn.getID());
row.addView(Delete);
layout.addView(row);
Log.d("Name: ", log);
}
答案 0 :(得分:2)
一起去:
Button place = new Button(this);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) place.getLayoutParams();
params.weight = 1.0f;
place.setLayoutParams(params);
place.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//handle the click
}
});
实例名称应以小写字母开头。