我想在AlertDialog中显示一个带有按钮的列表。按钮用于关闭Dialog本身。我扩展了AlertDialog
并向LayoutParams
添加了一些Window
以扩展整个屏幕宽度的对话框:
//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
,但按钮不会显示在它下面。这是对话框的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
内,无论如何都尝试过但失败了......)
答案 0 :(得分:6)
试一试:
<?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"
行。某些样式配置可能会更改视图样式并隐藏按钮。
答案 1 :(得分:0)
我想在AlertDialog中显示一个带有按钮的列表。
所以你需要做的是:
从按钮中删除它:
android:layout_below="@+id/choose_equipment_list"
然后把它放在Button:
android:layout_alignParentBottom="true"
并将其放入ListView:
android:layout_above="@+id/btn_choose_equipment_close"
你完成了。
答案 2 :(得分:0)
嗯。也许尝试使用Fragments。我给你编了样本。告诉我你是否正在寻找:)
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" />
答案 3 :(得分:0)
我认为你可以在XML中首先找到你需要声明的按钮,然后在它上方的listview中将height设置为fill_parent
答案 4 :(得分:0)
这应该是你想要的......
<?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>