我正在创建一个带有listview的弹出窗口。在列表视图中,每行包含一个文本框和单选按钮,但弹出窗口不显示。我无法弄清楚这个错误。
这是代码。
import java.util.List;
import java.util.Vector;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;
public class SettingsActivity extends Activity {
PopupWindow popup;
ListView fontSizeListView;
View popupView;
int currentFontSize;
Vector<FontSize> fontData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_view);
TextView tv = (TextView) findViewById(R.id.font_size_opt);
Typeface tf = TsciiTypeface.getTypeface(this);
tv.setTypeface(tf);
tv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showPopupMenu();
}
});
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
currentFontSize = Integer.parseInt(sp.getString("font_size", "1"));
SharedPreferences.Editor editor = sp.edit();
editor.clear();
editor.commit();
}
private void showPopupMenu() {
try {
popup = new PopupWindow(this);
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(R.layout.font_popup_view, null, false);
fontSizeListView = (ListView) popupView
.findViewById(R.id.font_size_list);
String[] arrLabel = getResources().getStringArray(
R.array.font_size_label_array);
String[] arrValue = getResources().getStringArray(
R.array.font_size_value_array);
fontData = new Vector<FontSize>();
for (int i = 0; i < arrLabel.length; i++) {
fontData.add(new FontSize(Integer.parseInt(arrValue[i]), arrLabel[i]));
}
CustomAdapter adapter = new CustomAdapter(this, R.layout.custom_list_pref_row, fontData);
fontSizeListView.setAdapter(adapter);
popup.setContentView(popupView);
popup.showAtLocation(this.findViewById(R.id.font_size_opt), Gravity.CENTER, 0, 0);
adapter.notifyDataSetChanged();
} catch (Exception e) {
MessageBox(e.toString());
}
}
private View getRowView(String label, int value,ViewGroup parent) {
try {
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.custom_list_pref_row, parent,false);
TextView text = (TextView) row
.findViewById(R.id.custom_list_view_row_text_view);
text.setText(label);
RadioButton rButton = (RadioButton) row
.findViewById(R.id.custom_list_view_row_radio_button);
rButton.setId(value);
row.setTag(value);
row.setClickable(true);
if (value == currentFontSize) {
rButton.setChecked(true);
}
row.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
return row;
} catch (Exception e) {
MessageBox(e.toString());
return null;
}
}
public void MessageBox(String message) {
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle("Error");
adb.setMessage(message);
adb.setPositiveButton("Ok", null);
adb.show();
}
class CustomAdapter extends ArrayAdapter<FontSize> {
public CustomAdapter(Context context, int resource, List<FontSize> objects) {
super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// widgets displayed by each item in your list
FontSize rowData = getItem(position);
if (null == convertView) {
convertView = getRowView(rowData.label,rowData.value,parent);
}
return convertView;
}
}
private class FontSize {
public int value;
public String label;
FontSize(int pvalue, String plabel){
value = pvalue;
label = plabel;
}
@Override
public String toString() {
return value + " " + label;
}
}
}
这是弹出窗口布局xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/popup_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="demo value"
/>
<ListView
android:layout_height="wrap_content"
android:id="@+id/font_size_list"
android:layout_width="wrap_content"></ListView>
</LinearLayout>
答案 0 :(得分:4)
我遇到了同样的问题,调用更新修复了问题。我怀疑弹出窗口的宽度和高度尚不清楚。所以在你的情况下,代码将是:
popup.showAtLocation(this.findViewById(R.id.font_size_opt), Gravity.CENTER, 0, 0);
popup.update(0, 0, popup_width, popup_height);
答案 1 :(得分:0)
我选择了here找到的教程。我想我发现了你的问题。在您的视图中:
popupView = inflater.inflate(R.layout.font_popup_view, null, false);
您将返回null值并返回false。不确定错误是什么,但是尝试执行ViewGroup
:
popupView = inflater.inflate(R.layout.font_popup_view,
(ViewGroup) findViewById(R.id.popup_layout));
希望这适合你。