我的应用程序中有一个包含微调器的对话框。该对话框旨在覆盖整个屏幕空间。虽然微调器只是对话框的一部分,但在选择微调器的下拉菜单时,下拉菜单覆盖了整个屏幕空间。请参考下面的图像以获得对话框参考:
现在点击微调按钮后,下拉列表如下图所示:
正如您所看到的,它覆盖了整个屏幕,因为我希望它的宽度限制在微调器宽度。下面我发布了微调器的代码:
对话框代码:
final Dialog dlg_dialog = new Dialog(myContext, R.style.Theme_Dialog);
// dialog.setCancelable(false);
dlg_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = dlg_dialog.getWindow();
if (window == null) return;
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dlg_dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.FILL_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
lp.gravity = Gravity.CENTER;
dlg_dialog.getWindow().setAttributes(lp);
//dialog.setContentView(R.layout.new_field_quote_entry_dialog);
dlg_dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dlg_dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
dlg_dialog.setContentView(R.layout.new_field_quote_entry_dialog);
// final Spinner spnr_spinnerSelectMarket = (Spinner)dlg_dialog.findViewById(R.id.spinnerSelectMarket);
TextView txtv_selectCompetitorDropDown = (TextView) dlg_dialog.findViewById(R.id.spinnerSelectCompetitorDropDown);
final Spinner spnr_spinnerSelectCompetitor = (Spinner) dlg_dialog.findViewById(R.id.spinnerSelectCompetitor);
spnr_spinnerSelectCompetitor.setClickable(false);
spnr_spinnerSelectCompetitor.setEnabled(false);
style.Dialog:
<style name="Theme_Dialog" parent="android:Theme.Dialog">
<item name="android:windowMinWidthMajor">100%</item>
<item name="android:windowAnimationStyle">@null</item>
<item name="android:windowMinWidthMinor">100%</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowIsFloating">false</item>
现在显示下拉列表,使用以下代码:
CustomSpinnerAdapter customSpinnerAdapterCompetitor = new CustomSpinnerAdapter(myContext,
R.layout.spinner_row, alst_competitor, "Select Competitor");
competitor_spinner.setAdapter(customSpinnerAdapterCompetitor);
现在为CustomSpinnerAdapter:
public class CustomSpinnerAdapter extends ArrayAdapter<String> {
Context context;
ArrayList<String> objects;
String firstElement;
boolean isFirstTime;
public CustomSpinnerAdapter(Context context, int textViewResourceId, ArrayList<String> objects, String defaultText) {
super(context, textViewResourceId, objects);
this.context = context;
this.objects = objects;
this.isFirstTime = true;
setDefaultText(defaultText);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if(isFirstTime) {
objects.set(0, firstElement);
isFirstTime = false;
}
return getCustomDropdownView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
notifyDataSetChanged();
return getCustomView(position, convertView, parent);
}
public void setDefaultText(String defaultText) {
this.firstElement = objects.get(0);
objects.set(0,defaultText);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.spinner_row, parent, false);
TextView label = (TextView) row.findViewById(R.id.spinnerText);
label.setText(objects.get(position));
return row;
}
public View getCustomDropdownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.simple_spinner_dropdown_item, parent, false);
CheckedTextView label = (CheckedTextView) row.findViewById(R.id.text1);
label.setText(objects.get(position));
return row;
}
}
如果您需要任何其他代码参考,请告诉我们。我无法解决此问题。请帮帮我。提前谢谢。
答案 0 :(得分:0)
/Just remove the below lines of code/
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dlg_dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.FILL_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
lp.gravity = Gravity.CENTER;
dlg_dialog.getWindow().setAttributes(lp);