【问题标题】:Android How do I add some floating button over my custom ListView?Android 如何在我的自定义 ListView 上添加一些浮动按钮?
【发布时间】:2011-03-25 13:20:58
【问题描述】:

我已经完成了我的自定义 ListView,xml 如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
  <ListView android:id="@+id/list" android:layout_width="1px" android:layout_height="1px" android:layout_weight="1"></ListView>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent">
      <ImageView android:id="@+id/img" android:layout_width="26px" android:layout_height="30px" />
      <TextView android:id="@+id/title" android:layout_width="248dp" android:layout_height="wrap_content" android:textStyle="bold" android:paddingLeft="6dp" android:textSize="20sp" />
      <ImageButton android:id="@+id/cat_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:layout_marginRight="2dp" />
      <ImageButton android:id="@+id/act_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="4dp" />
  </LinearLayout>
</LinearLayout>

' 而代码是

public class MyListActivity extends ListActivity
{
  protected void onCreate(Bundle savedInstanceState)
  {
    Log.i("MyApp", "ON List create......");
    super.onCreate(savedInstanceState);
    List<BeanObj> list=...//Do something to fill list
    setListAdapter(new MyListViewAdapter(context, R.layout.item_view, list));
  }

  public class MyListViewAdapter extends ArrayAdapter<List<BeanObj>>
  {
    private List<BeanObj> hList=null;
    public MyListViewAdapter(Context context, int textViewResourceId, List hList)
    {
      super(context, textViewResourceId, hList);
      this.hList=hList;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
      final BeanObj bean=this.hList.get(position);
      LayoutInflater inflater=getLayoutInflater();
      View row=inflater.inflate(R.layout.item_view, parent, false);
      // Do something to fill ListView
      return row;
    }
  }
}

我确实尝试在其上添加一个按钮,但我在列表视图的每一行中都获得了每个按钮...

我可以在 xml 上做什么以在我的自定义列表视图上添加浮动按钮? 或者,我在下面创建另一个 xml 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="left|top">
  <Button android:id="@+id/btnLogout" android:layout_width="wrap_content" android:layout_height="40dp" android:text="@string/buttonLogout" />
  <View android:layout_width="75dp" android:layout_height="40dp" />
  <Button android:id="@+id/btnList" android:layout_width="wrap_content" android:layout_height="40dp" android:text="@string/buttonList" />
  <Button android:id="@+id/btnSearch" android:layout_width="wrap_content" android:layout_height="40dp" android:text="@string/buttonSearch" />
</LinearLayout>

如何通过 addHeaderView 或 addFooterView 方法使用它?

【问题讨论】:

    标签: android listview layout button


    【解决方案1】:

    在为列表设置 ListAdapter 之前,您需要添加页眉和页脚。来自docs

    注意:在调用之前调用它 设置适配器。这样 ListView 可以 用一个包裹提供的光标 还将考虑标题和 页脚视图。

    在 getView 方法中膨胀视图也不是最佳方式。您需要使用静态类来存储对膨胀视图的引用。查看thistalk 以正确设置视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-11
      • 2018-04-13
      • 2015-01-01
      相关资源
      最近更新 更多