安卓:列表视图阵列适配器没有正确包装内容阵列、适配器、视图、正确

2023-09-12 05:59:38 作者:痞子

我有一个Theme.Dialog的活动,presents的设备列表给用户。该活动使用一个ListView具有动态创建ArrayAdapter显示设备的列表。出于某种原因,然而,在ListView(以及因此的活动本身)不调整包裹它的内容正确。相反,它的作用就好像它有大约一半的屏幕设定宽度(这仍然存在跨设备具有不同的屏幕尺寸)。我不能为我的身影在哪里这个设置,虽然生活。

I have a Theme.Dialog activity that presents a list of devices to a user. the activity uses a ListView with a dynamically created ArrayAdapter to display the list of devices. For some reason however, the ListView (and thus the activity itself) isn't resizing to wrap it's content correctly. Instead, it acts as if it's got a set width of about half the screen (and this persists across devices with different screen sizes). I can't for the life of my figure out where this is set, though.

有人能看到我缺少的是什么? (我试图剥离不相关的位出以下code)

Can anyone see what I'm missing? (I've tried to strip the irrelevent bits out of the following code)

下面是我的活动:

public class DeviceListActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Setup the window
        setContentView(R.layout.device_list);

        // Initialize array adapters
        mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);

        // Find and set up the ListView for paired devices
        ListView pairedListView = (ListView) findViewById(R.id.paired_devices);
        pairedListView.setAdapter(mPairedDevicesArrayAdapter);

        //the following doesn't do anything
        //getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        mPairedDevicesArrayAdapter.add("asdfasdfa");        

    }
}

下面是device_list.xml:

Here is device_list.xml:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <ListView
        android:id="@id/paired_devices"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:stackFromBottom="true" 
        android:background="#0000FF"/>
        <!-- the colour shows the listview stretching wider than the text it contains -->


</LinearLayout>

下面是我的Andr​​oidManifest.xml中的相关部分:

Here's the relevant portion of my AndroidManifest.xml:

<activity
     android:name=".DeviceListActivity"
     android:configChanges="keyboardHidden|orientation"
     android:label="too wide"
     android:theme="@android:style/Theme.Dialog">
</activity>

而这里的device_name.xml(这是用来初始化一个ArrayAdapter:

And here's device_name.xml (which is used to initialize the ArrayAdapter:

<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5.0dip"
    android:textSize="18.0sp" />

如果你坚持看完的问题到现在为止,我很感激了。

And if you've stuck with reading the question until now, I'm grateful already.

下面是最终的结果(跟我在后台的应用程序的其余部分):

Here's the end result (with the rest of my app in the background):

推荐答案

我意识到,这基本上是一个封闭的问题,但我相信,通过设置windowIsFloating到真正的主题,在这个答案的