如何在单击 cardview 时膨胀视图?
how to inflate a view on click of cardview?
想知道如何在单击 CardView
时膨胀这种类型的视图。以及图像中这样的角如何设计?
提前致谢
在 OnClick 中你可以显示一个 PopupWindow
。像创建任何其他布局一样为其创建 xml 布局,然后在 onCreateView
/onCreate
中像这样膨胀视图
View popupView = layoutInflater.inflate(R.layout.popup, null);
。
从那里您只需创建和配置 PopupWindow。
PopupWindow popup = new PopupWindow(popupView);
然后在您的 onClickListener 调用中 popup.showAsDropDown(anchor);
。
关于拐角,您应该可以使用 square/rectangle 形状在 xml 中创建它,然后旋转它来制作三角形。从那里将其包含在您的 R.layout.popup
中
查看 https://developer.android.com/reference/android/widget/PopupWindow.html 了解它的 API 以及如何配置它。
我使用了 PopupWindow ,它解决了我的问题
final PopupWindow popup = new PopupWindow();
View layout = getLayoutInflater(savedInstanceState).inflate(R.layout.popup_layout, null);
try {
final Button b1 = (Button) layout.findViewById(R.id.cancel_action);
final Button b2 = (Button) layout.findViewById(R.id.quantity_action);
final Button b3 = (Button) layout.findViewById(R.id.link_action);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
paymentoptionedt.setText("Cash");
Toast.makeText(getContext(),"Cash",Toast.LENGTH_SHORT).show();
popup.dismiss();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
paymentoptionedt.setText("Card");
Toast.makeText(getContext(),"Card",Toast.LENGTH_SHORT).show();
popup.dismiss();
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
paymentoptionedt.setText("Link Pay");
Toast.makeText(getContext(),"Link Pay",Toast.LENGTH_SHORT).show();
popup.dismiss();
}
});
}catch (Exception e){
e.printStackTrace();
}
popup.setContentView(layout);
popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popup.setOutsideTouchable(true);
popup.setFocusable(true);
popup.setBackgroundDrawable(new BitmapDrawable());
popup.showAsDropDown(view);
想知道如何在单击 CardView
时膨胀这种类型的视图。以及图像中这样的角如何设计?
提前致谢
在 OnClick 中你可以显示一个 PopupWindow
。像创建任何其他布局一样为其创建 xml 布局,然后在 onCreateView
/onCreate
中像这样膨胀视图
View popupView = layoutInflater.inflate(R.layout.popup, null);
。
从那里您只需创建和配置 PopupWindow。
PopupWindow popup = new PopupWindow(popupView);
然后在您的 onClickListener 调用中 popup.showAsDropDown(anchor);
。
关于拐角,您应该可以使用 square/rectangle 形状在 xml 中创建它,然后旋转它来制作三角形。从那里将其包含在您的 R.layout.popup
中查看 https://developer.android.com/reference/android/widget/PopupWindow.html 了解它的 API 以及如何配置它。
我使用了 PopupWindow ,它解决了我的问题
final PopupWindow popup = new PopupWindow();
View layout = getLayoutInflater(savedInstanceState).inflate(R.layout.popup_layout, null);
try {
final Button b1 = (Button) layout.findViewById(R.id.cancel_action);
final Button b2 = (Button) layout.findViewById(R.id.quantity_action);
final Button b3 = (Button) layout.findViewById(R.id.link_action);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
paymentoptionedt.setText("Cash");
Toast.makeText(getContext(),"Cash",Toast.LENGTH_SHORT).show();
popup.dismiss();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
paymentoptionedt.setText("Card");
Toast.makeText(getContext(),"Card",Toast.LENGTH_SHORT).show();
popup.dismiss();
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
paymentoptionedt.setText("Link Pay");
Toast.makeText(getContext(),"Link Pay",Toast.LENGTH_SHORT).show();
popup.dismiss();
}
});
}catch (Exception e){
e.printStackTrace();
}
popup.setContentView(layout);
popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popup.setOutsideTouchable(true);
popup.setFocusable(true);
popup.setBackgroundDrawable(new BitmapDrawable());
popup.showAsDropDown(view);