我正在显示PopupWindow并将其背景设置为9补丁图像。 PopupWindows背景在可见时变为透明。我找不到问题。应该如何解决?
这是我的代码。
LayoutInflater inflater = LayoutInflater.from(context);
View panel = inflater.inflate(R.layout.add_collection, null);
panel.setLayoutParams(new ViewGroup.LayoutParams
(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
panel.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int status_bar_height = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height" ,
"dimen" , "android");
if(resourceId > 0){
status_bar_height = context.getResources().getDimensionPixelSize(resourceId);
}
popupWindow = new PopupWindow(panel,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
Drawable drawable = ContextCompat.getDrawable(context, R.drawable.menu_popup_bg);
popupWindow.setBackgroundDrawable(drawable);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
popupWindow.setElevation(48);
}
popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
popupWindow.setFocusable(true);
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.showAtLocation(anchor, Gravity.TOP | Gravity.END, 0,
status_bar_height);
这是我的add_collection.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/_10sdp"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/_6sdp"
android:layout_marginTop="@dimen/_4sdp"
android:text="Add New Collection"
android:textColor="@color/white"
android:textSize="@dimen/_11sdp" />
<com.suntec.oneread.customviews.OREditText
android:id="@+id/et_collection_name"
android:layout_width="match_parent"
android:layout_height="@dimen/_24sdp"
android:background="@drawable/edittext_border"
android:ems="10"
android:gravity="center_vertical"
android:hint="Collection Name"
android:inputType="textCapWords|textCapSentences"
android:maxLength="15"
android:maxLines="1"
android:minWidth="@dimen/_160sdp"
android:paddingLeft="@dimen/_4sdp"
android:paddingRight="@dimen/_4sdp"
android:textColor="@color/text_color"
android:textColorHint="@color/text_color"
android:textSize="@dimen/_10sdp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_10sdp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/_4sdp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/btnSave"
android:layout_width="match_parent"
android:layout_height="@dimen/_25sdp"
android:background="@color/btn_background"
android:text="Save"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="@dimen/_12sdp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/_4sdp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/btnCancel"
android:layout_width="match_parent"
android:layout_height="@dimen/_25sdp"
android:background="@color/btn_background"
android:text="Cancel"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="@dimen/_12sdp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
,这是menu_popup_bg.xml文件。我将drawable设置为9个补丁图像。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_selected="true"
android:drawable="@drawable/menu_bg_light_grey" />
<item android:state_selected="true" android:drawable="@drawable/menu_bg_light_grey" />
<item android:state_pressed="true" android:state_selected="false"
android:drawable="@drawable/menu_bg_light_grey" />
<item android:state_selected="false" android:drawable="@drawable/menu_bg_light_grey" />
</selector>