我正在尝试创建一个扩展DialogFragment
的自定义对话框,
当我创建一个简单的对话框时,微调框下拉列表可以很好地工作,并且在尝试打开它时会做出反应。
在DialogFragment
上,微调器会很好地显示第一个选项,但是当我单击他时,它不会打开下拉菜单。
我决定转到基于DialogFragment
的对话框,因为在屏幕旋转时,对话框的大小不适合横向模式。
在我的应用程序中,我显示了来自自定义ArrayAdapter
的对话框,因此没有找到一种在屏幕旋转时更改对话框大小的方法,因此我认为DialogFragment
是解决我的问题的合理方法
微调框下拉菜单在简单的对话框上时效果很好,移至DialogFragment
时不改变任何内容。
DialogFragment类的代码:
@Override
public void onResume() {
super.onResume();
ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
params.height = ViewGroup.LayoutParams.MATCH_PARENT;
getDialog().getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
}
“ treatmentTypes”是Spinner的私有成员。
'GetTypesOfTreatments'是具有用于从服务器下载数据和更新微调器的函数的类。 该功能运行良好,我可以在日志中看到微调器获得了所需的一切。
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d(TAG, "onCreateView: In Dialog");
String fullDate, fullTime;
user.getUserDetails(view);
Button btnCancel, btnOK;
ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
int displayWidth = params.width;
String min = currentDate.getMin() < 10 ? currentDate.getMin() + "0" : Integer.toString(currentDate.getMin());
fullDate = currentDate.getDay() + "/" + currentDate.getMonth() + "/" + currentDate.getYear();
fullTime = currentDate.getHour() + ":" + min;
//...
dialog_date = (TextView) view.findViewById(R.id.dialog_new_apt_date);
dialog_time = (TextView) view.findViewById(R.id.dialog_new_apt_time);
dialog_date.setText(fullDate);
dialog_time.setText(fullTime);
treatmentTypes = (Spinner) view.findViewById(R.id.dialog_new_apt_type_list);
treatmentTypes.setDropDownWidth((int) (displayWidth * 0.5f));
btnCancel = (Button) view.findViewById(R.id.dialog_new_apt_cancel);
btnOK = (Button) view.findViewById(R.id.dialog_new_apt_ok);
username = (TextView) view.findViewById(R.id.dialog_new_apt_edit_name);
phoneNumber = (TextView) view.findViewById(R.id.dialog_new_apt_edit_phone);
GetTypesOfTreatments getTypesOfTreatments = new GetTypesOfTreatments();
getTypesOfTreatments.getListOfTypes(this.getContext(), treatmentTypes);
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
....
}
GetTypesOfTreatments更新微调器部分:(微调器变量是我从对话框中传递的内容)
SpinnerAdapter adapter = new SpinnerAdapter(context,R.layout.adapter_spinner_treatment_types,typeList);
spinner.setAdapter(adapter);
SpinnerAdapter:
@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return createItemView(position,convertView,parent);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return createItemView(position,convertView,parent);
}
private View createItemView(int position, View convertView, ViewGroup parent){
final View view = layoutInflater.inflate(resources, parent, false);
view.setBackgroundColor(view.getResources().getColor(R.color.colorBackground,null));
TextView description = (TextView) view.findViewById(R.id.spinner_adapter_description),
price = (TextView) view.findViewById(R.id.spinner_adapter_price);
description.setText(list.get(position).getDescription());
if(list.get(position).getPrice() != 0)
price.setText(Integer.toString(list.get(position).getPrice()));
return view;
}
对话框布局代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:theme="@android:style/Theme.Material.Dialog"
android:layout_height="match_parent">
<android.support.constraint.Guideline
... />
<TextView
... />
<EditText
... />
<TextView
... />
<Button
... />
<android.support.constraint.Guideline
... />
<Spinner
android:id="@+id/dialog_new_apt_type_list"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:dropDownWidth="150dp"
android:gravity="center|start"
android:spinnerMode="dropdown"
android:textAlignment="gravity"
android:textDirection="locale"
app:layout_constraintBottom_toTopOf="@+id/guideline7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline19"
app:layout_constraintTop_toTopOf="@+id/guideline6" />
<android.support.constraint.Guideline
... />
<TextView
... />
<android.support.constraint.Guideline
... />
<TextView
... />
<android.support.constraint.Guideline
... />
<TextView
... />
<EditText
... />
<Button
... />
<android.support.constraint.Guideline
... />
<TextView
... />
</android.support.constraint.ConstraintLayout>
谢谢大家的帮助。