【问题标题】:Button under ListView in AlertDialog is hiddenAlertDialog中ListView下的按钮被隐藏
【发布时间】:2014-02-10 20:26:13
【问题描述】:

我想在 AlertDialog 中显示一个带有按钮的列表。该按钮是关闭对话框本身。我扩展了AlertDialog并在Window中添加了一些LayoutParams来扩展整个屏幕宽度的Dialog:

    //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);

好的。但如果列表很长(显示已满),我可以滚动ListView,但按钮不会显示在其下方。这是 Dialog 的 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>

我该怎么做才能在列表下始终显示按钮,无论它有多长? (我读了很多警告,将ListView 放在ScrollView 中,无论如何都尝试过但失败了......)

【问题讨论】:

  • 在警报对话框上使用setPositiveButton,这就是它的用途。

标签: android listview scrollview scrollable


【解决方案1】:

试试看:

<?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_button" 行。一些样式配置可能会改变视图样式并隐藏按钮。

【讨论】:

  • 它完成了这项工作(没有删除我需要的样式;))抱歉,没有测试其他答案,因为这个答案有效,但感谢大家的所有答案!
  • 很好的答案,谢谢!如果有人 android:layout_height="0dp" 给出错误,您可以改用 wrap_content,它仍然有效
【解决方案2】:

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

所以你要做的是:

从您的 Button 中删除它:

android:layout_below="@+id/choose_equipment_list"

然后把这个放到Button里面:

android:layout_alignParentBottom="true"

并将其放入 ListView:

android:layout_above="@+id/btn_choose_equipment_close"

你已经完成了。

【讨论】:

    【解决方案3】:

    嗯。也许尝试使用片段。我为您编写了示例代码。告诉我它是否是你要找的:)

    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();
        }
    }
    

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

    【讨论】:

      【解决方案4】:

      我认为有可能获得您需要在 XML 中声明的内容,您的按钮首先位于底部,然后是其上方的列表视图,高度设置为 fill_parent

      【讨论】:

        【解决方案5】:

        这应该是你想要的......

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

        【讨论】:

          猜你喜欢
          • 2020-11-03
          • 1970-01-01
          • 2014-01-13
          • 2020-04-01
          • 2012-12-08
          • 2011-09-04
          • 2017-01-31
          • 2021-03-21
          • 1970-01-01
          相关资源
          最近更新 更多