将第二个屏幕的大小限制为wrap_content

时间:2015-06-24 07:33:40

标签: android android-layout

在Android应用中,我有第二个屏幕,其中只包含ListView。通过单击主屏幕中的Button来显示它,并在用户单击项目时关闭。

问题是我的第二个屏幕中有一个相对较大的空白区域并且看起来不太好。

问题是:如何限制第二个屏幕的大小,使其看起来像DialogBoxListView

分机:ListView只是一个例子,第二个屏幕的内容可能是Compound View这样的内容很复杂但不会填满整个屏幕。因此,以XML编程实现XML中的第二个屏幕非常重要。

我的第二个屏幕的XML代码:

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

    <ListView
        android:id="@+id/myListView2"
        android:layout_width="300dp"
        android:layout_height="300dp"
        >
    </ListView>
</LinearLayout>

- 我尝试将第二个屏幕的layout_widthlayout_height限制为wrap_content300dp,但不适用于它们。

感谢您的回答!

3 个答案:

答案 0 :(得分:0)

在onCLick中使用此代码

AlertDialog.Builder builderSingle = new AlertDialog.Builder(
                DialogActivity.this);
        builderSingle.setIcon(R.drawable.ic_launcher);
        builderSingle.setTitle("Select One Name:-");
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                DialogActivity.this,
                android.R.layout.select_dialog_singlechoice);
        arrayAdapter.add("Hardik");
        arrayAdapter.add("Archit");
        arrayAdapter.add("Jignesh");
        arrayAdapter.add("Umang");
        arrayAdapter.add("Gatti");
        builderSingle.setNegativeButton("cancel",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

        builderSingle.setAdapter(arrayAdapter,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String strName = arrayAdapter.getItem(which);
                        AlertDialog.Builder builderInner = new AlertDialog.Builder(
                                DialogActivity.this);
                        builderInner.setMessage(strName);
                        builderInner.setTitle("Your Selected Item is");
                        builderInner.setPositiveButton("Ok",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(
                                            DialogInterface dialog,
                                            int which) {
                                        dialog.dismiss();
                                    }
                                });
                        builderInner.show();
                    }
                });
        builderSingle.show();

这将在对话框中显示列表视图

有关详情,请点击here

答案 1 :(得分:0)

Finally I could find the answer!

The only thing to do is to set the android.theme to Dialog in the AndroidManifest, like this:

    <activity android:name=".SecondActivity"
              android:label="secondAct"
              android:theme="@android:style/Theme.Holo.Dialog"

Compared to AlertDialog.Builder, this method has the advantage of customizing the secondary layout without any limitation in terms of the type of views. And the secondary layout should not be created programmatically.

答案 2 :(得分:-1)

您的列表视图没有内容 尝试适配器