Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为放置在对话框中并动态添加元素的ListView设置固定大小_Java_Android_Xml_Listview_Size - Fatal编程技术网

Java 如何为放置在对话框中并动态添加元素的ListView设置固定大小

Java 如何为放置在对话框中并动态添加元素的ListView设置固定大小,java,android,xml,listview,size,Java,Android,Xml,Listview,Size,我试图在对话框中运行的活动中包含列表视图。该对话框基本上用于搜索蓝牙设备,因此我需要两个按钮,一个文本视图和一个列表视图。以下是我的XML: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation=&quo

我试图在
对话框中运行的
活动
中包含
列表视图
。该对话框基本上用于搜索蓝牙设备,因此我需要两个
按钮,一个
文本视图
和一个
列表视图
。以下是我的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"
  android:padding="20dp">
    <TextView
        android:text="Bluetooth List"
        android:paddingTop="15dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">
    </TextView>
    <ListView android:id="@+id/bluetoothPanel_lvBluetooth"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_margin="10dip"
        android:drawSelectorOnTop="false">
    </ListView>
    <LinearLayout
        android:orientation="horizontal"
        android:paddingLeft="4.0dip"
        android:paddingTop="5.0dip"
        android:paddingRight="4.0dip"
        android:paddingBottom="1.0dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
            <Button
                android:id="@+id/bluetoothPanel_btnSearch"
                android:layout_width="0.0dip"
                android:layout_height="fill_parent"
                android:text="Search"
                android:layout_weight="1.0" />
            <Button
                android:id="@+id/bluetoothPanel_btnCancel"
                android:layout_width="0.0dip"
                android:layout_height="fill_parent"
                android:text="Cancel"
                android:layout_weight="1.0" />
    </LinearLayout>
</LinearLayout>

开始时,
ListView
中的
ArrayAdapter
是空的,我正在动态地向它添加元素

问题是,我想为
ListView
设置一个固定的大小,这样它在动态添加元素时不会改变大小(显示蓝牙设备列表)。你能帮我修复我的XML吗

编辑:


我不是说数组大小。我说的是listview本身(视图),listview的高度。

为列表设置一个自定义ListAdapter,并覆盖
getCount()
方法,使其始终返回固定大小


示例:

我想您是在问如何为动态变化的ListView保留固定大小的空间。在类似的情况下,我将ListView包装在一个LinearLayout中,如下所示。请注意,这是ListView本身的xml,而不是单元格内容的xml。本例中的固定高度为100dip x 200dip

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/background_image"
    android:layout_width="100dip"
    android:layout_height="200dip">

    <ListView   android:id="@+id/list_view"
                android:layout_width="100dip"
                android:layout_height="match_parent"
                android:cacheColorHint="#00000000"
    />
</LinearLayout>

请注意,ListView的布局宽度也设置为所需的固定宽度。我不知道为什么,但这似乎是必要的

还要注意,背景图像或颜色是在LinearLayout上设置的,而不是在ListView上设置的。将ListView的cacheColorHint设置为transparent可以使ListView很好地播放


如果您在问其他问题(例如,可能不考虑元素而固定可滚动高度,这似乎由@Stephen回答),您可能需要澄清这个问题。

哦,我不是在说数组大小。我说的是listview本身(视图),通常视图大小取决于数组大小。如果阵列大小固定,视图将保持不变。但是我使用的阵列不是固定的,这取决于周围有多少蓝牙设备:)啊,好的,误解了。如果您想这样做,可以为列表设置固定高度:例如,
layout\u width=“50dip”
我以前尝试过这个方法,但它不会改变大小,可能是因为我在对话框上运行了活动。