不显示Android的弹出窗口 [英] android popup window not displaying

查看:169
本文介绍了不显示Android的弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个列表视图的弹出窗口。在ListView中每一行包含一个文本框和单选按钮,但弹出窗口不显示。我想不通的错误。

这是code。

 进口的java.util.List;
进口java.util.Vector中;
进口android.app.Activity;
进口android.app.AlertDialog;
进口android.content.Context;
进口android.content.Shared preferences;
进口android.graphics.Typeface;
进口android.os.Bundle;
。进口的Andr​​oid preference preferenceManager。
进口android.view.Gravity;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;
进口android.widget.PopupWindow;
进口android.widget.RadioButton;
进口android.widget.TextView;
公共类SettingsActivity延伸活动{PopupWindow弹出;
ListView的fontSizeListView;
查看popupView;
INT currentFontSize;
矢量<&字号GT; fontData;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.settings_view);
    TextView的电视=(的TextView)findViewById(R.id.font_size_opt);
    字体TF = TsciiTypeface.getTypeface(本);
    tv.setTypeface(TF);    tv.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            showPopupMenu();        }
    });    共享preferences SP = preferenceManager
            .getDefaultShared preferences(本);
    currentFontSize =的Integer.parseInt(sp.getString(FONT_SIZE,1));
    共享preferences.Editor编辑= sp.edit();
    editor.clear();
    editor.commit();
}私人无效showPopupMenu(){
    尝试{
        弹出=新PopupWindow(本);
        LayoutInflater吹气=(LayoutInflater)本
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        popupView = inflater.inflate(R.layout.font_popup_view,空,假);
        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 =新的矢量<&字号GT;();
        的for(int i = 0; I< arrLabel.length;我++){
            fontData.add(新字号(的Integer.parseInt(arrValue [I]),arrLabel [I]));
        }        CustomAdapter适配器=新CustomAdapter(这一点,R.layout.custom_list_ pref_row,fontData);
        fontSizeListView.setAdapter(适配器);
        popup.setContentView(popupView);
        popup.showAtLocation(this.findViewById(R.id.font_size_opt),Gravity.CENTER,0,0);         adapter.notifyDataSetChanged();    }赶上(例外五){
        的MessageBox(e.toString());
    }}私人查看getRowView(字符串标签,int值,ViewGroup中父){
    尝试{
        LayoutInflater吹气=(LayoutInflater)本
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        查看排= inflater.inflate(R.layout.custom_list_ pref_row,父母,假);
        TextView的文本=(TextView的)行
                .findViewById(R.id.custom_list_view_row_text_view);
        text.setText(标签);
        单选rButton =(单选)行
                .findViewById(R.id.custom_list_view_row_radio_button);
        rButton.setId(值);
        row.setTag(值);
        row.setClickable(真);        如果(价值== currentFontSize){
            rButton.setChecked(真);
        }        row.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(视图v){            }
        });        返回行;
    }赶上(例外五){
        的MessageBox(e.toString());
        返回null;
    }
}公共无效的MessageBox(字符串消息){
    AlertDialog.Builder ADB =新AlertDialog.Builder(本);
    adb.setTitle(错误);
    adb.setMessage(消息);
    adb.setPositiveButton(OK,NULL);
    adb.show();
}类CustomAdapter扩展ArrayAdapter<&字号GT; {    公共CustomAdapter(上下文的背景下,INT资源列表与LT;字号>对象){
        超级(上下文,资源,对象);    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){        //在你的列表中每个项目显示的部件        字号rowData =的getItem(位置);        如果(空== convertView){
            convertView = getRowView(rowData.label,rowData.value,父母);        }
        返回convertView;
    }
}私有类字号{
    公共int值;
        公共字符串标签;        字号(INT P值,字符串PLABEL){
            值= P值;
            标签= PLABEL;
    }        @覆盖
        公共字符串的toString(){
                返回值++标签;
        }
}}

这是弹出窗口布局XML

 <?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
机器人:ID =@ + ID / popup_layout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
<的TextView
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=试玩值
 />
< ListView控件
    机器人:layout_height =WRAP_CONTENT
    机器人:ID =@ + ID / font_size_list
    机器人:layout_width =WRAP_CONTENT>< /&的ListView GT;
 < / LinearLayout中>


解决方案

我有同样的问题,并呼吁更新解决了这一问题。我怀疑,在弹出的宽度和高度都还不知道。所以,你的情况code将是:

  popup.showAtLocation(this.findViewById(R.id.font_size_opt),Gravity.CENTER,0,0);
  popup.update(0,0,popup_width,popup_height);

I am creating a popup window with a listview. In the listview each row contains a textbox and radio button but the popup window not displaying. I can't figure out the mistake.

This is the code.

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

}

This is the popup window layout 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>

解决方案

I had the same problem and calling update fixed the problem. I suspect that the width and height of the popup are not yet known. So in your case the code would be:

  popup.showAtLocation(this.findViewById(R.id.font_size_opt), Gravity.CENTER, 0, 0);
  popup.update(0, 0, popup_width, popup_height);

这篇关于不显示Android的弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆