我正在开发一个需要通过蓝牙与其他设备配对的应用程序.我遇到了麻烦,以正确的方式显示配对设备的列表.我也在android api(蓝牙聊天)中看过这个例子,但我遇到了同样的问题.
配对设备列表将变大,然后隐藏位于列表底部的搜索按钮.
我的xml代码与示例非常相似:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/listpaired_devices"
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"
/>
<ListView android:id="@+id/paired_devices"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stackFromBottom="true"
android:layout_weight="1"
/>
<TextView android:id="@+id/list_new_devices"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/title_other_devices"
android:visibility="gone"
/>
<ListView android:id="@+id/new_devices"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stackFromBottom="true"
android:layout_weight="2"
/>
<Button android:id="@+id/btscan"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btscan"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
但是我无法在底部显示搜索按钮.我的屏幕: 我的屏幕
您可以在对话框窗口的底部看到一些按钮.
可以限制列表视图中显示的行数吗?任何人都可以告诉我如何解决这个问题
首先是关于你的代码的几点:
layout_weight
仅当对象在某个维度中没有大小(即您设置layout_height
或layout_width
0 )时才有意义,因此这对您的代码没有影响.wrap_content
为非常无意义,即使它起作用也是不好的做法.使用'fill_parent'或确定的高度.所以让我们考虑一下你真正拥有的东西 - 它只是一个单独的列表,底部有一个按钮(是的,你可能有标题或多个数据源,但我们会进入那个).
以下布局将在顶部提供ListView,在底部提供Button.ListView将占用Button未使用的任何空间.注意如何在布局中的ListView 之前定义Button - 这会导致Android在考虑ListView之前计算按钮占用的高度.然后它为ListView提供剩余的高度.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/btscan"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/btscan"
/>
<ListView android:id="@+id/all_devices"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/btscan"
android:stackFromBottom="true"
/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
这就是布局.现在让我们考虑列表的实际内容:您有一个标题,后面跟着一个配对设备列表,然后是另一个标题,然后是新设备列表.
您可以使用单个适配器创建它 - 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<View> firstHeaderViews = new List<View();
firstHeaderViews.add(myHeaderView1);
SackOfViewsAdapter firstHeaderAdapter = new SackOfViewsAdapter(firstHeaderViews)
// Second header
List<View> secondHeaderViews = new List<View();
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)
Run Code Online (Sandbox Code Playgroud)
产生的布局应该是这样的.注意我扩展了列表,以显示列表长于屏幕的行为; 按钮仍然可见.红色框标记屏幕的边界.
归档时间: |
|
查看次数: |
12272 次 |
最近记录: |