问题描述
下午好,我在 popupview 内部点击,它正在关闭,我希望的行为是点击事件只会通过点击外部触发,但是正如你所看到的,弹出窗口占据了整个屏幕,是问题?
如您所见,弹出窗口占据了整个屏幕,背景是透明的,但是如果我在弹出窗口内部单击它会关闭 popup
如何使用 ConstraintLayout 将弹出窗口居中?
代码弹出
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:background="#80AAAAAA"
android:layout_margin="40dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:layout_margin="20dp"
android:id="@+id/pop_up_enviar_ocorrencias_card_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:cardCornerRadius="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
android:padding="20dp"
app:layout_constraintHorizontal_bias="0.0">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/pop_up_ocorrencias_constraint_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp">
<TextView
android:id="@+id/pop_up_ocorrencias_txt_cracha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="marcações"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<ListView
android:id="@+id/pop_up_enviar_ocorrencias_list_marcacoes"
android:layout_width="0dp"
tools:listitem="@layout/item_marcacao"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="@id/pop_up_ocorrencias_txt_cracha"
app:layout_constraintTop_toBottomOf="@id/pop_up_ocorrencias_txt_cracha"/>
<TextView
android:id="@+id/pop_up_enviar_ocorrencias_txt_ocorrencias"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ocorrências"
app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_list_marcacoes"
app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_list_marcacoes" />
<TextView
android:id="@+id/pop_up_enviar_ocorrencias_txt_ocorrencias_api"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_txt_ocorrencias"
app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_txt_ocorrencias" />
<TextView
android:id="@+id/pop_up_enviar_ocorrencias_txt_motivo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Motivo"
app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_txt_ocorrencias_api"
app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_txt_ocorrencias_api" />
<EditText
android:id="@+id/pop_up_enviar_ocorrencias_edt_motivos_api"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Motivo"
app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_txt_motivo"
app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_txt_motivo" />
<TextView
android:id="@+id/pop_up_enviar_ocorrencias_txt_descricao"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Descrição"
app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_edt_motivos_api"
app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_edt_motivos_api" />
<EditText
android:id="@+id/pop_up_enviar_ocorrencias_edt_descricao_api"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Descrição da ocorrência"
app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_txt_descricao"
app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_txt_descricao" />
<TextView
android:id="@+id/pop_up_enviar_ocorrencias_txt_foto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Foto"
app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_edt_descricao_api"
app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_edt_descricao_api" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/pop_up_enviar_ocorrencias_constraint_foto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_txt_foto"
app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_txt_foto">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/pop_up_enviar_ocorrencias_txt_selecione_imagem"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Selecione uma imagem"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/pop_up_enviar_ocorrencias_image_selecione_imagem"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/pop_up_enviar_ocorrencias_image_selecione_imagem"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/ic_cselect_image"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/pop_up_enviar_ocorrencias_constraint_foto_vizualizar_foto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_constraint_foto"
app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_constraint_foto">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/pop_up_enviar_ocorrencias_txt_vizualizar_foto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:breakStrategy="high_quality"
android:text="Vizualizar Foto"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
<ImageView
android:id="@+id/pop_up_enviar_ocorrencias_image_vizualizar_foto"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/ic_arrow_down"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/pop_up_enviar_ocorrencias_enviar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enviar"
android:background="@drawable/button_round"
app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_constraint_foto_vizualizar_foto"
app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_constraint_foto_vizualizar_foto" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
编码我的类弹出
public class PopUpOcorrencia {
//referencia https://medium.com/@evanbishop/popupwindow-in-android-tutorial-6e5a18f49cc7
@RequiresApi(api = Build.VERSION_CODES.Q)
@SuppressLint("ClickableViewAccessibility")
public void showPopupWindow(final View view){
//Crie você mesmo um objeto View através do inflater
LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.pop_up_enviar_ocorrencia,null);
// Especifique o comprimento e a largura por meio de constantes
int width = ConstraintLayout.LayoutParams.MATCH_PARENT;
int height = ConstraintLayout.LayoutParams.MATCH_PARENT;
// Tornar itens inativos fora da PopupWindow
boolean focusable = true;
// Crie uma janela com nossos parâmetros
final PopupWindow popupWindow = new PopupWindow(popupView,width,height,focusable);
// Defina a localização da janela na tela
popupWindow.showAtLocation(view,Gravity.CENTER,0);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#80AAAAAA")));
popupWindow.setFocusable(true);
popupWindow.setoutsidetouchable(true);
//popupWindow.setTouchModal(true);
// Inicialize os elementos de nossa janela,instale o manipulador
TextView test2 = popupView.findViewById(R.id.pop_up_enviar_ocorrencias_txt_ocorrencias_api);
test2.setText("Teste");
Button buttonEdit = popupView.findViewById(R.id.pop_up_enviar_ocorrencias_enviar);
buttonEdit.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//As an example,display the message
Toast.makeText(view.getContext(),"Wow,popup action button",Toast.LENGTH_SHORT).show();
}
});
// Manipulador para clicar na zona inativa da janela
popupView.setonTouchListener((View v,MotionEvent event) -> {
//Close the window when clicked
popupWindow.dismiss();
return true;
});
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)