我正在开发一个需要通过蓝牙与其他设备配对的应用程序.我遇到了麻烦,以正确的方式显示配对设备的列表.我也在android api(蓝牙聊天)中查看了这个例子,但我遇到了同样的问题.
配对设备列表将变大,然后隐藏位于列表底部的搜索按钮.
我的xml代码与示例非常相似:
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/title_paired_devices"
android:visibility="gone"
android:background="#666"
android:textColor="#fff"
android:paddingLeft="5dp"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stackFromBottom="true"
android:layout_weight="1"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/title_other_devices"
android:visibility="gone"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stackFromBottom="true"
android:layout_weight="2"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btscan"
/>
但是我无法在底部显示搜索按钮.
在这里我的屏幕:
My Screen
您可以在对话框窗口的底部看到一些按钮.
可以限制列表视图中显示的行数吗?任何人都可以告诉我如何解决这个问题
解决方法:
首先是关于你的代码的几点:
> layout_weight仅在对象在特定维度中没有大小时才有意义,即将layout_height或layout_width设置为0,因此这对您的代码没有影响.
>将ListView的高度设置为wrap_content是没有意义的,即使它起作用也是不好的做法.使用’fill_parent’或确定的高度.
>按钮被隐藏,因为根据上面的点,您创建的ListView没有预定义的大小,因此占用尽可能多的空间,将按钮从屏幕上移开.
所以让我们考虑一下你真正拥有的东西 – 它只是一个单独的列表,底部有一个按钮(是的,你可能有标题或多个数据源,但我们会进入那个).
以下布局将在顶部提供ListView,在底部提供Button. ListView将占用Button未使用的任何空间.注意如何在布局中的ListView之前定义Button – 这会导致Android在考虑ListView之前计算按钮占用的高度.然后它为ListView提供剩余的高度.
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/btscan"
/>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/btscan"
android:stackFromBottom="true"
/>
这就是布局.现在让我们考虑列表的实际内容:您有一个标题,后面跟着一个配对设备列表,然后是另一个标题,然后是新设备列表.
您可以使用单个适配器创建它 – Commonsware提供了一个非常好的实现,称为MergeAdapter,但您可以编写自己的代码. MergeAdapter不会直接为您提供视图(例如标题),但幸运的是Commonsware还提供了允许您添加视图的SackOfViewsAdapter,然后您可以将SackOfViewsAdapter添加到MergeAdapter.
这是一些伪代码,应该完成上面概述的内容.
// This is the adapter we will use for our list with ListView.setAdapter
MergeAdapter listAdapter = new MergeAdapter();
// The first header - you'll need to inflate the actual View (myHeaderView1) from some resource
// using LayoutInflater
List firstHeaderViews = new List
firstHeaderViews.add(myHeaderView1);
SackOfViewsAdapter firstHeaderAdapter = new SackOfViewsAdapter(firstHeaderViews)
// Second header
List secondHeaderViews = new List
secondHeaderViews.add(myHeaderView2);
SackOfViewsAdapter secondHeaderAdapter = new SackOfViewsAdapter(secondHeaderViews);
// Now to add everything to the MergeAdapter
// First goes the first header
listAdapter.addAdapter(firstHeaderAdapter);
// Now your first list
listAdapter.addAdapter(firstListAdapter);
// Now your second header
listAdapter.addAdapter(secondHeaderAdapter);
// Now your second list
listAdapter.addAdapter(secondListAdapter);
// Now set the adapter to the list
myList.setAdapter(listAdapter)
生成的布局应该看起来像like this.注意我扩展了列表以显示它的行为与列表长于屏幕的行为;按钮仍然可见.红色框标记屏幕的边界.
标签:android,listview
来源: https://codeday.me/bug/20190729/1576654.html