如何在listview中为每行添加按钮

如何在listview中为每行添加按钮,第1张

你是的意思是在listView的每一项里都加一个固定的按钮或图片吗,那就将它写到一个xml中,然后inflate到listView中,这样listView的每一项的布局就都是xml中的布局了。

<LinearLayout xmlns:android="

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<Button

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button"

/>

<ListView

android:id="@+id/listView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>

</LinearLayout>

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState)

setContentView(R.layout.listview)

String[] data = {"a", "b", "c", "d", "a", "b", "c", "d", "a", "b", "c", "d"}

ListView listView = (ListView) findViewById(R.id.listView)

listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, data))

在布局器中将按钮都加进去,然后再代码中根据情况来设置按钮是否可见: setVisibility(View.INVISIBLE) 给按钮添加一个listener,就可以得到点击事件。 希望能帮助你!


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11572287.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存