我最近在PopupWindow中设计了一个菜单。应该允许用户选择日期和时间。当用户选择一个项目时,背景应更改为浅蓝色,文本应更改为黑色。然后,如果用户再次单击,则该项目的背景应切换回白色,文本变为灰色。
这在模拟器上效果很好。我已经在Nexus 6和Pixel 3 XL上进行了测试。
但是,在物理设备上会发生一些奇怪的事情。当我单击某个项目时,其背景有时会消失,显示其背后的活动。其他时候,它会闪烁为蓝色背景,甚至会更改屏幕不同部分区域的背景。尽管如此,在适当更改背景的情况下,它有时仍会按预期工作。测试两部手机时,我会遇到这种行为:小型摩托罗拉和大型三星。
为解决此问题,我尝试了多种解决方案。例如,我玩过基本的PopupWindow选项,例如setOutsideTouchable和focusable。我还摆弄了自己的布局,例如为每个单独的项目设置不同的背景,并在通货膨胀期间分配根。另外,我尝试将变量设置为全局弹出窗口,并在后台更改后重置某些功能。但是,这些尝试均未产生任何结果。该窗口仍然可以在仿真器上正常运行,但不能在实际设备上运行。
感谢您的帮助。以下是指向物理设备上行为的屏幕截图以及代码段的链接。提前致谢。
https://imgur.com/a/1K9pSHi上的屏幕截图
1。设置弹出窗口的Java代码
public void showDurationPopup(View v) {
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.post_duration_popup,null);
addDayOpsToViews(inflater,layout);
addHourOpsToViews(inflater,layout);
final PopupWindow popup = new PopupWindow(layout, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT, false);
popup.setOutsideTouchable(false);
popup.update();
popup.showAtLocation(layout, Gravity.CENTER, 0, 0);
setPopupFinishBtnListener(layout,popup);
}
private void addDayOpsToViews(LayoutInflater inflater,View popupLayout){
LinearLayout daysListView = popupLayout.findViewById(R.id.daysOpsList);
View newItem;
// Set items for days 1-29
for(int d = 1;d<=29;d++){
newItem = inflater.inflate(R.layout.duration_item,null);
TextView contentView = newItem.findViewById(R.id.durItemContent);
contentView.setText(Integer.toString(d));
daysListView.addView(newItem);
setDurItemListener(daysListView,newItem,d-1,0);
}
// Load in previously selected pos
adjustDurItemBackgrounds(daysListView,0);
}
private void addHourOpsToViews(LayoutInflater inflater,View popupLayout){
View newItem;
LinearLayout hoursListView = popupLayout.findViewById(R.id.hoursOpsList);
// Set items for hours 0-23
for(int h = 0;h<=23;h++){
newItem = inflater.inflate(R.layout.duration_item,null);
TextView contentView = newItem.findViewById(R.id.durItemContent);
contentView.setText(Integer.toString(h));
hoursListView.addView(newItem);
setDurItemListener(hoursListView,newItem,h,1);
}
// Load in previously selected pos
adjustDurItemBackgrounds(hoursListView,1);
}
2。用于onClick响应的Java代码
// Set onClickListener for an individual duration item
// Type 0 is day, type 1 is hour
private void setDurItemListener(final View listView,
final View newItem, final int position, final int type){
newItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!(selDurationPos.get(type) == position)) {
selDurationPos.set(type, position);
}
else{
selDurationPos.set(type, -1);
}
adjustDurItemBackgrounds((ViewGroup)listView,type);
}
});
}
private void adjustDurItemBackgrounds(ViewGroup listView,int type){
int numChildren = listView.getChildCount();
View currChild;
for(int c=0; c<numChildren; c++){
currChild = listView.getChildAt(c);
if(c==selDurationPos.get(type)){
((TextView)currChild.findViewById(R.id.durItemContent)).setTextColor(Color.BLACK);
currChild.setBackgroundColor(Color.CYAN);
}
else{
((TextView)currChild.findViewById(R.id.durItemContent)).setTextColor(Color.GRAY);
currChild.setBackgroundColor(Color.WHITE);
}
}
}
3。单个项目的布局(天或小时)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="@color/white"
>
<TextView
android:id="@+id/durItemContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="@color/gray"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/blue"
/>
</LinearLayout>
4。弹出窗口的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="@+id/daysWrapper"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginStart="30dp"
android:orientation="vertical"
android:layout_toStartOf="@+id/vertSepLine"
android:background="@color/white"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
android:touchscreenBlocksFocus = "false"
>
<TextView
android:id="@+id/daysTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Days"
android:background="@color/white"
android:textStyle="bold"
android:textColor="@color/blue"
android:textSize="22sp"
android:layout_gravity="center_horizontal"
/>
<TextView
android:id="@+id/vertULine1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/cyanBlue" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
>
<LinearLayout
android:id="@+id/daysOpsList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/white"
>
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:id="@+id/hoursWrapper"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginEnd="30dp"
android:layout_toEndOf="@+id/vertSepLine"
android:orientation="vertical"
android:background="@color/white"
android:layout_alignTop="@id/daysWrapper"
android:touchscreenBlocksFocus = "false"
>
<TextView
android:id="@+id/hoursTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hours"
android:background="@color/white"
android:layout_gravity="center_horizontal"
android:textStyle="bold"
android:textColor="@color/blue"
android:textSize="22sp"
/>
<TextView
android:id="@+id/vertULine2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/cyanBlue" />
<ScrollView
android:layout_width="match_parent"
android:background="@color/white"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="@+id/hoursOpsList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/white"
>
</LinearLayout>
</ScrollView>
</LinearLayout>
<TextView
android:id="@+id/leftVertSep"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_alignTop="@id/daysWrapper"
android:layout_above="@id/finishButton"
android:layout_alignStart="@id/horizSepLine"
android:background="@color/cyanBlue" />
<TextView
android:id="@+id/rightVertSep"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_alignTop= "@+id/horizSepLine"
android:layout_above="@id/finishButton"
android:layout_alignEnd="@id/horizSepLine"
android:background="@color/cyanBlue" />
<TextView
android:id="@+id/vertSepLine"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_alignTop= "@+id/horizSepLine"
android:layout_centerHorizontal="true"
android:layout_above="@id/finishButton"
android:background="@color/cyanBlue" />
<TextView
android:id="@+id/horizSepLine"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_above="@+id/daysWrapper"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:background="@color/cyanBlue" />
<Button
android:id="@+id/finishButton"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:text="Finish"
android:gravity="center"
android:textStyle="bold"
android:textColor="@color/white"
android:background="@color/blue"
android:layout_below="@+id/hoursWrapper"
/>
</RelativeLayout>
5。包装布局(简化):
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".PostCreationNew.QuestionChallengeCreation"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/durationWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:layout_below="@+id/question_deadline">
<TextView
android:id="@+id/durationTitleV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Duration*"
android:layout_marginStart="10dp"
android:textColor="@color/black"
android:layout_marginBottom="10dp"
android:textStyle="bold"
/>
<ImageView
android:id="@+id/durationHintBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_error_outline_grey_24dp"
android:layout_toEndOf="@+id/durationTitleV"
android:layout_alignTop="@+id/durationTitleV"
android:onClick="showDurationHint"
/>
<RelativeLayout
android:id="@+id/durationBody"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_below="@+id/durationTitleV"
android:onClick="showDurationPopup"
>
<RelativeLayout
android:id="@+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/currDurInfoView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/blue"
android:layout_marginStart="10dp"
android:textStyle="bold"
android:textSize="22sp"
android:visibility="gone"
android:onClick="showDurationPopup"
android:drawableEnd="@drawable/ic_arrow"
/>
<TextView
android:id="@+id/question_duration_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="click here"
android:paddingLeft="10dp"
android:layout_marginBottom="10dp"
android:layout_below="@id/currDurInfoView"
/>
</RelativeLayout>
<TextView
android:id="@+id/underline"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_alignBottom="@+id/wrapper"
android:layout_alignStart="@+id/wrapper"
android:layout_alignEnd="@+id/wrapper"
android:background="@color/blue"
/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>