Android:listview数组适配器未正确包装内容

时间:2012-12-20 23:51:19

标签: android layout android-activity android-arrayadapter

我有一个Theme.Dialog活动,它向用户显示设备列表。活动使用带有动态创建的ArrayAdapter的ListView来显示设备列表。但是出于某种原因,ListView(以及活动本身)没有调整大小以正确包装它的内容。相反,它的作用就好像它的屏幕宽度大约是屏幕的一半(并且这会在具有不同屏幕尺寸的设备上持续存在)。不过,我不能为我的生活找到合适的地方。

任何人都能看到我错过的东西吗? (我试图从以下代码中删除无关的位)

这是我的活动:

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:

<?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>

以下是我的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:

<?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" />

如果你一直坚持阅读这个问题,我已经很感激了。

这是最终结果(我的应用程序的其余部分在后台): I can't figure out why the dialog isn't wrapping the text.

3 个答案:

答案 0 :(得分:1)

我意识到这本质上是一个封闭的问题,但我相信通过在主题中将windowIsFloating设置为true,如此答案https://stackoverflow.com/a/8876358所示,您的问题将得到解决。

答案 1 :(得分:0)

我很确定这是因为android:theme="@android:style/Theme.Dialog",那些讨厌的对话框有自己的宽度和高度。

请尝试以下操作:创建一个代表FragmentListActivity的{​​{1}}。将其放在其他片段上会产生类似对话框的行为。

我100%确定问题是Theme.Dialog。您最好的镜头是改变您显示该列表的策略。 (例如,使用片段)

答案 2 :(得分:0)

我终于能够让这个工作..i子类化Listview并改变它的测量方式。看来listview本身只会衡量第一个孩子。所以它使用第一个孩子的宽度。我将其更改为在listviews中搜索最宽行。

所以对话框内容的xml如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<com.mypackage.myListView
    android:id="@+id/lv"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
 />

,myListView看起来像这样:

public class MyListView extends ListView{

public MyListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stub
    int maxWidth = meathureWidthByChilds() + getPaddingLeft() + getPaddingRight();
    super.onMeasure(MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.EXACTLY), heightMeasureSpec);     
}


 public int meathureWidthByChilds() {
    int maxWidth = 0;
    View view = null;
    for (int i = 0; i < getAdapter().getCount(); i++) {
        view = getAdapter().getView(i, view, this);
        //this measures the view before its rendered with no constraints from parent
        view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        if (view.getMeasuredWidth() > maxWidth){
            maxWidth = view.getMeasuredWidth();
        }
    }
    return maxWidth;
 }
}
相关问题