gpt4 book ai didi

java - 如何设置以编程方式创建的按钮的属性?

转载 作者:行者123 更新时间:2023-11-29 19:29:18 26 4
gpt4 key购买 nike

如何设置以编程方式创建的按钮的属性?例如:在我下面的示例中,我想为我的“放置”按钮创建一个“android:layout_weight="1""和一个"on_click="startsearch""属性。

    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);
}

最佳答案

选择:

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
}
});

实例名称应以小写字母开头。

关于java - 如何设置以编程方式创建的按钮的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40472580/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com