0

デバイスのリストをユーザーに表示する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" />

そして、あなたが今まで質問を読み続けることに固執しているなら、私はすでに感謝しています。

最終結果は次のとおりです(アプリの残りの部分をバックグラウンドで表示します)。 ダイアログがテキストを折り返さない理由がわかりません。

4

3 に答える 3

1

私はこれが本質的に閉じた質問であることを理解していますが、この回答https://stackoverflow.com/a/8876358に見られるように、テーマでwindowIsFloatingをtrueに設定することで、問題が修正されると思います。

于 2013-06-21T23:53:46.667 に答える
0

android:theme="@android:style/Theme.Dialog"独自の幅と高さを持つ厄介なダイアログのせいであると確信しています。

次のことを試してください。自分をとしてFragment表すを作成します。これを他のフラグメントの上に配置すると、ダイアログのような動作が作成されます。ListActivityListview

問題はTheme.Dialogだと100%確信しています。あなたのベストショットは、そのリストを表示するための戦略を変えることでしょう。(たとえば、フラグメントを使用)

于 2012-12-21T00:15:07.890 に答える
0

私はついにこれを機能させることができました..私はListviewをサブクラス化し、測定方法を変更しました。リストビューはネイティブで最初の子だけを測定するようです。したがって、最初の子の幅を使用します。リストビューの子で最も幅の広い行を検索するように変更しました。

したがって、ダイアログコンテンツの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;
 }
}
于 2014-06-21T01:45:12.997 に答える