Android AlertDialog中ListView下的按钮被隐藏

Android AlertDialog中ListView下的按钮被隐藏,android,listview,scrollview,scrollable,Android,Listview,Scrollview,Scrollable,我想在AlertDialog中显示一个带有按钮的列表。该按钮用于关闭对话框本身。我扩展了AlertDialog,并在窗口中添加了一些LayoutParams,以扩展整个屏幕宽度的对话框: //Grab the window of the dialog, and change the width WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); Window window = getWindo

我想在AlertDialog中显示一个带有按钮的列表。该按钮用于关闭对话框本身。我扩展了
AlertDialog
,并在
窗口中添加了一些
LayoutParams
,以扩展整个屏幕宽度的对话框:

    //Grab the window of the dialog, and change the width
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    Window window = getWindow();
    lp.copyFrom(window.getAttributes());
    //This makes the dialog take up the full width
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
    window.setAttributes(lp);
嗯。但是如果列表很长(显示屏上满是),我可以滚动
列表视图
,但按钮不会显示在它下面。以下是该对话框的ContentView:

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

<ListView
    android:id="@+id/choose_equipment_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:layout_below="@+id/choose_equipment_list"
    android:id="@+id/btn_choose_equipment_close"
    style="@style/custom_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="close" />

</RelativeLayout>

我该怎么做才能使按钮始终显示在列表下,无论它有多长?(我读了很多警告,将
列表视图
放在
滚动视图
中,但还是尝试了,失败了…

尝试:

<?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/choose_equipment_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <Button
        android:id="@+id/btn_choose_equipment_close"
        style="@style/custom_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="close" />

</LinearLayout>

如果不起作用,请删除
style=“@style/custom_按钮”
行。某些样式配置可能会更改视图样式并隐藏按钮。

请尝试:

<?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/choose_equipment_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <Button
        android:id="@+id/btn_choose_equipment_close"
        style="@style/custom_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="close" />

</LinearLayout>

如果不起作用,请删除
style=“@style/custom_按钮”
行。某些样式配置可能会更改视图样式并隐藏按钮

我想在AlertDialog中显示一个带有按钮的列表

所以你要做的是:

从按钮中删除此选项:

android:layout_below="@+id/choose_equipment_list"
然后将此按钮放入:

android:layout_alignParentBottom="true"
并将其放在ListView中:

android:layout_above="@+id/btn_choose_equipment_close"
你完了

我想在AlertDialog中显示一个带有按钮的列表

所以你要做的是:

从按钮中删除此选项:

android:layout_below="@+id/choose_equipment_list"
然后将此按钮放入:

android:layout_alignParentBottom="true"
并将其放在ListView中:

android:layout_above="@+id/btn_choose_equipment_close"

您完成了。

嗯。也许可以尝试使用片段。我为你们编码了样品。告诉我这是否是你想要的:)

MainActivity.java

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        findViewById(R.id.open_dialog_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SampleDialogFragment dialogFragment = new SampleDialogFragment();
                dialogFragment.show(getSupportFragmentManager(), "dialogFragment");
            }
        });
    }
}
main_activity.xml

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:id="@+id/open_dialog_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open dialog"
        android:layout_gravity="center" />
</FrameLayout>

SampleDialogFragment.java

public class SampleDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
        alert.setTitle("Timer");
        alert.setPositiveButton("OK", null);

        View view = View.inflate(getActivity(), R.layout.dialog, null);

        String[] array = {"Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, array);
        ((ListView) view.findViewById(R.id.listView)).setAdapter(adapter);

        alert.setView(view);

        return alert.create();
    }
}
public类SampleDialogFragment扩展了DialogFragment{
@凌驾
创建对话框上的公共对话框(Bundle savedInstanceState){
AlertDialog.Builder alert=新建AlertDialog.Builder(getActivity());
警报。设置标题(“计时器”);
alert.setPositiveButton(“确定”,空);
视图=视图.充气(getActivity(),R.layout.dialog,null);
字符串[]数组={“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”};
ArrayAdapter=新的ArrayAdapter(getActivity(),android.R.layout.simple\u list\u item\u 1,数组);
((ListView)view.findViewById(R.id.ListView)).setAdapter(adapter);
alert.setView(视图);
返回alert.create();
}
}
dialog.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

嗯。可能尝试使用片段。我为你们编码了样品。告诉我这是否是你想要的:)

MainActivity.java

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        findViewById(R.id.open_dialog_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SampleDialogFragment dialogFragment = new SampleDialogFragment();
                dialogFragment.show(getSupportFragmentManager(), "dialogFragment");
            }
        });
    }
}
main_activity.xml

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:id="@+id/open_dialog_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open dialog"
        android:layout_gravity="center" />
</FrameLayout>

SampleDialogFragment.java

public class SampleDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
        alert.setTitle("Timer");
        alert.setPositiveButton("OK", null);

        View view = View.inflate(getActivity(), R.layout.dialog, null);

        String[] array = {"Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, array);
        ((ListView) view.findViewById(R.id.listView)).setAdapter(adapter);

        alert.setView(view);

        return alert.create();
    }
}
public类SampleDialogFragment扩展了DialogFragment{
@凌驾
创建对话框上的公共对话框(Bundle savedInstanceState){
AlertDialog.Builder alert=新建AlertDialog.Builder(getActivity());
警报。设置标题(“计时器”);
alert.setPositiveButton(“确定”,空);
视图=视图.充气(getActivity(),R.layout.dialog,null);
字符串[]数组={“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”、“你好”};
ArrayAdapter=新的ArrayAdapter(getActivity(),android.R.layout.simple\u list\u item\u 1,数组);
((ListView)view.findViewById(R.id.ListView)).setAdapter(adapter);
alert.setView(视图);
返回alert.create();
}
}
dialog.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


我认为有可能在XML中获得您需要声明的内容您的按钮首先位于底部,然后在其上方的listview中设置高度以填充父对象

我认为有可能获得您需要在XML中声明的内容您的按钮首先位于底部,然后在其上方的listview中设置高度填充家长这应该是你想要的

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

<ListView
    android:id="@+id/choose_equipment_list"
    android:layout_above="@+id/btn_choose_equipment_close"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/btn_choose_equipment_close"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="close" />

</RelativeLayout>

这应该是您想要的

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

<ListView
    android:id="@+id/choose_equipment_list"
    android:layout_above="@+id/btn_choose_equipment_close"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/btn_choose_equipment_close"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="close" />

</RelativeLayout>


在alertdialog上使用
setPositiveButton
,这就是它的用途。在alertdialog上使用
setPositiveButton
,这就是它的用途。它完成了任务(没有删除样式,顺便说一句;))抱歉,没有测试其他答案,因为这一个有效,但感谢所有答案!!回答得很好,谢谢!如果有人
android:layout\u height=“0dp”
出现错误,您可以使用
wrap\u content
替代,它仍然可以工作它完成了工作(没有删除样式,顺便说一句,我需要它;))抱歉,没有测试其他答案,因为这一个有效,但感谢所有答案!!回答得很好,谢谢!如果有人
android:layout\u height=“0dp”
出现错误,您可以改为使用
wrap\u content
,它仍然有效