2011-12-07 3 views
2

Я создаю всплывающее окно со списком. В списке в каждой строке содержится текстовое поле и переключатель, но всплывающее окно не отображается. Я не могу понять ошибку.всплывающее окно Android не отображается

Это код.

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

Как насчет использования Alert.Dialog вместо всплывающего окна, он должен работать. Сделали это несколько раз. –

ответ

4

У меня была такая же проблема, и проблема с вызовом исправила проблему. Я подозреваю, что ширина и высота всплывающего окна еще не известны. Таким образом, в вашем случае код будет выглядеть следующим образом:

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

видно, что на каком-то устройстве всплывающее окно не имеет правильного размера. должен вызвать update(), чтобы сделать его всплывающим. –

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

Надеется, что это работает для вас.

Смежные вопросы