我有一个包含自定义布局的弹出窗口,其中包含文件项列表。我想根据自定义布局的高度为弹出窗口设置高度。我试过了
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fileName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#303030"
android:orientation="vertical" >
<TextView
android:id="@+id/newfile"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:text="first item"
android:textColor="@color/white"
android:drawableLeft="@drawable/zw_new"
android:drawablePadding="5dp"
android:padding="10dp"
android:textSize="20sp" />
<View style="@style/hrView" />
<TextView
android:id="@+id/rename"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:text="second item"
android:textColor="@color/white"
android:drawableLeft="@drawable/zw_rename"
android:drawablePadding="5dp"
android:padding="10dp"
android:textSize="20sp" />
</LinearLayout>
style.xml
<style name="hrView">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">1dp</item>
<item name="android:background">#4f4f4f</item>
</style>
代码:
public static PopupWindow fileMenu= null;
public void showfileMenu(){
View fileMenuView=null;
fileMenuView = getLayoutInflater().inflate (R.layout.zw_filemenu, null);
fileMenuView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int h = fileMenuView.getMeasuredHeight();
int w = fileMenuView.getMeasuredWidth();
fileMenu=showPopup(fileMenuView,w,h);
}
PopupWindow pw=null;
public PopupWindow showPopup(View view,int w,int h) {
pw = new PopupWindow(EditorActivity.getActivity());
pw.setWidth(w);
pw.setHeight(h);
pw.setTouchable(true);
pw.setFocusable(true);
pw.setOutsideTouchable(true);
pw.setTouchInterceptor(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
pw.dismiss();
return true;
}
return false;
}
});
pw.setOutsideTouchable(false);
pw.setContentView(view);
return pw;
}
我的高度为106,宽度为191.因此,我的弹出窗口仅显示第一个文本视图“第一个项目”(太破碎),第二个项目因高度较低而被隐藏。请帮我。
答案 0 :(得分:0)
这可能会帮助您创建像
这样的流行音乐private PopupWindow mpopup;
View popUpView = getLayoutInflater().inflate(R.layout.yourxml, null); // inflating popup layout
mpopup = new PopupWindow(popUpView, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, true); // Creation of popup
mpopup.setAnimationStyle(R.style.DialogAnimation);
mpopup.showAtLocation(popUpView, Gravity.BOTTOM, 0, 0); // Displaying
我在这里给出LayoutParams.FILL_PARENT
,LayoutParams.WRAP_CONTENT