我做了一个弹出窗口,工作正常,但我想写上TextView。尽管Eclipse找到了TextView的id并且没有显示任何问题,但使用标准方法不起作用(只是崩溃了)。弹出窗口采用另一种XML布局。
生成弹出窗口
public PopupWindow pw;
public void popUpShow() {
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
pw = new PopupWindow(
inflater.inflate(R.layout.popup, null, false),
400,
600,
true);
pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0);
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_layout"
android:orientation="vertical"
android:padding="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#AAA"
>
<TextView
android:id="@+id/popupOut"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text=""
/>
<Button
android:id="@+id/popupclose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/close"
android:onClick="popUpHide" />
</LinearLayout>
答案 0 :(得分:1)
在创建PopupWindow之前,请保持对视图Inflated
的引用ViewGroup v = (ViewGroup)inflater.inflate(R.layout.popup, null, false);
pw = new PopupWindow(
v,
400,
600,
true);
pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0);
TextView view = (TextView)v.findViewById(R.id.textView1);
答案 1 :(得分:0)
如果textView在对话框中,则应通过
检索视图IDDialog dialog = new Dialog(this);
TextView view = (TextView)dialog.findViewById(R.id.textView1);
尝试
public void popUpShow() {
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout t = (LinearLayout) inflater.inflate(R.layout.item1, null, false);
PopupWindow pw = new PopupWindow(
t,
400,
600,
true);
pw.showAtLocation(t.findViewById(R.id.main), Gravity.CENTER, 0, 0);
}