Je souhaite créer une boîte de dialogue personnalisée avec la mise en page, comme indiqué dans l'image. Le bouton de croix/fermeture doit être en haut à droite. Veuillez suggérer comment obtenir ce type de présentation.
Merci d'avance.
peut-être que le code ci-dessous vous est utile, réalisé comme ceci
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_gravity="center_vertical"
Android:gravity="center"
Android:orientation="vertical" >
<FrameLayout
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_centerInParent="true" >
<LinearLayout
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_marginRight="5dp"
Android:layout_marginTop="10dp"
Android:background="@drawable/popup_password_latest"
Android:gravity="center"
Android:orientation="vertical"
Android:paddingBottom="-50dp" >
<!---add your views here-->
</LinearLayout>
<ImageView
Android:id="@+id/imageView_close"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_gravity="top|right"
Android:clickable="true"
Android:src="@drawable/close_selector" />
</FrameLayout>
</LinearLayout>
Disposition relative sera votre parent, puis ajoutera le bouton de fermeture, appliquera Android:layout_alignParentRight="true"
et Android:layout_alignParentTop="true"
et donnera même marge droite et supérieure en moins selon vos besoins.
Code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent" >
<LinearLayout
Android:id="@+id/relativeLayout1"
Android:layout_width="255dp"
Android:layout_height="385dp"
Android:layout_centerInParent="true"
Android:background="@color/white"
Android:orientation="vertical" >
</LinearLayout>
<Button
Android:id="@+id/button_close"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_alignRight="@+id/relativeLayout1"
Android:layout_alignTop="@+id/relativeLayout1"
Android:layout_marginRight="-10dp"
Android:layout_marginTop="-10dp"
Android:background="@drawable/close" />
</RelativeLayout>
Sortie
Ici, j'ai une solution pour ça.
<FrameLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="match_parent" >
<FrameLayout
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_gravity="center" >
<FrameLayout
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:padding="15dp" >
<LinearLayout
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_gravity="center"
Android:background="@drawable/img_popup_one"
Android:orientation="vertical"
Android:padding="10dp" >
<TextView
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text="Sign In"
Android:textColor="@color/clr_gray"
Android:textSize="16dp" />
<EditText
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:layout_marginTop="15dp"
Android:background="@drawable/img_edittext_bg"
Android:hint="Username"
Android:padding="5dp"
Android:singleLine="true"
Android:textColorHint="@color/clr_gray" />
<EditText
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:layout_marginTop="15dp"
Android:background="@drawable/img_edittext_bg"
Android:hint="Password"
Android:inputType="textPassword"
Android:padding="5dp"
Android:singleLine="true"
Android:textColorHint="@color/clr_gray" />
</LinearLayout>
</FrameLayout>
<ImageView
Android:layout_width="30dp"
Android:layout_height="30dp"
Android:layout_gravity="right|top"
Android:contentDescription="@string/contentDescription"
Android:src="@drawable/img_popup_close" />
</FrameLayout>
Vous pouvez utiliser PopupWindow ici. Vous pouvez créer une présentation pour votre boîte de dialogue personnalisée et la gonfler dans PopupWindow. Elle devrait ressembler à ceci: -
PopupWindow menuPopup;
menuView=getLayoutInflater().inflate(R.layout.layout_menu, null);
menuPopup=new PopupWindow(menuView, 200, 200, false);
menuPopup.showAtLocation(menuView, Gravity.TOP | Gravity.RIGHT, 0, 100);
![anybody can try above all but u will be facing problem background color problem
i have done very simple way
<?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"
Android:background="@Android:color/transparent"
>
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginBottom="50dp"
Android:layout_marginLeft="50dp"
Android:layout_marginRight="50dp"
Android:layout_marginTop="50dp"
Android:background="#FFFFFF"
Android:gravity="center"
Android:orientation="horizontal" >
<LinearLayout
Android:layout_width="120dp"
Android:layout_height="match_parent"
Android:layout_gravity="center"
Android:gravity="center"
Android:orientation="vertical" >
<ImageView
Android:id="@+id/left_arrow_imageview"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:src="@drawable/up_arrow" />
<ListView
Android:id="@+id/listview_observation"
Android:layout_width="match_parent"
Android:layout_height="0dp"
Android:layout_gravity="center"
Android:layout_weight="2"
Android:padding="6dp" >
</ListView>
<ImageView
Android:id="@+id/right_arrow_imageview"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:src="@drawable/down_arrow" />
</LinearLayout>
<View
Android:layout_width="1dp"
Android:layout_height="match_parent"
Android:layout_gravity="center"
Android:layout_marginLeft="3dp"
Android:layout_marginRight="3dp"
Android:background="#B2B2B2"
Android:scrollY="@dimen/activity_vertical_margin" />
<LinearLayout
Android:layout_width="0dp"
Android:layout_height="match_parent"
Android:layout_weight="2"
Android:gravity="right"
Android:orientation="vertical" >
<TextView
Android:id="@+id/text_head"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_gravity="left"
Android:padding="6dp"
Android:text="@string/Observation"
Android:textSize="18sp"
Android:textStyle="bold" />
<View
Android:layout_width="match_parent"
Android:layout_height="1dp"
Android:layout_gravity="center"
Android:layout_marginLeft="3dp"
Android:layout_marginRight="3dp"
Android:background="#B2B2B2"
Android:scrollY="@dimen/activity_vertical_margin" />
<EditText
Android:id="@+id/edit_observation"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:layout_weight="1"
Android:background="#00000000"
Android:ems="10"
Android:gravity="top" >
</EditText>
<View
Android:layout_width="match_parent"
Android:layout_height="1dp"
Android:layout_gravity="center"
Android:layout_marginLeft="3dp"
Android:layout_marginRight="3dp"
Android:background="#B2B2B2"
Android:scrollY="@dimen/activity_vertical_margin" />
<Button
Android:id="@+id/button_done"
Android:layout_width="94dp"
Android:layout_height="wrap_content"
Android:layout_marginRight="14dp"
Android:layout_marginTop="3dp"
Android:background="@drawable/rectangular_view_selected"
Android:text="@string/button_done"
Android:textColor="@Android:color/white" />
</LinearLayout>
</LinearLayout>
<ImageView
Android:id="@+id/icon_close"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_alignParentRight="true"
Android:layout_alignParentTop="true"
Android:layout_marginRight="35dp"
Android:layout_marginTop="35dp"
Android:src="@drawable/icon_close" />
</RelativeLayout>
the dialog must be like that
private void ChildSectionView() {
final Dialog dialog = new Dialog(NewObservationActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog_obs_children);
// Grab the window of the dialog, and change the width
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(Android.graphics.Color.TRANSPARENT));
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = dialog.getWindow();
lp.copyFrom(window.getAttributes());
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
lp.width = size.x;
lp.height = size.y;
// This makes the dialog take up the full width
window.setAttributes(lp);
gridview = (GridView) dialog.findViewById(R.id.gridview_observation);
ImageView icon_close = (ImageView) dialog.findViewById(R.id.icon_close);
child_count = (TextView) dialog
.findViewById(R.id.text_child_count_dialog);
child_count.setText("Selected " + intList.size() + " more children");
SearchView searchview = (SearchView) dialog
.findViewById(R.id.search_children);
icon_close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
searchview.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onQueryTextChange(String string) {
filItemAdapter("N", string);
return false;
}
});
filItemAdapter("", "");
dialog.show();
}
Remarque :-------
the relative layout background should be transparent and dialog background also dialog.getWindow().setBackgroundDrawable( new ColorDrawable(Android.graphics.Color.TRANSPARENT));][2]
Utilisez ceci pour un dialogue personnalisé ...
private PopupWindow pw;
@SuppressWarnings("deprecation")
private void initiatePopupWindow() {
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup,
(ViewGroup) findViewById(R.id.btncancelcat));
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.width = WindowManager.LayoutParams.FILL_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
pw = new PopupWindow(layout, lp.width, lp.height, true);
pw.showAtLocation(layout, Gravity.CENTER_VERTICAL, 0, 0);
ImageButton btncancel = (ImageButton) layout.findViewById(R.id.btncancelcat);
btncancel.setOnClickListener(cancel_click);
}
private OnClickListener cancel_click = new OnClickListener() {
public void onClick(View v) {
pw.dismiss();
}
};
popup.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:id="@+id/linearpopup"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:background="#e22e2e"
Android:orientation="vertical"
Android:padding="10dp" >
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:background="@Android:color/darker_gray" >
</LinearLayout>
<ImageButton
Android:id="@+id/btncancelcat"
Android:layout_width="50dp"
Android:layout_height="50dp"
Android:layout_alignParentRight="true"
Android:layout_alignParentTop="true"
Android:src="@drawable/btn" />
</RelativeLayout>
Ajoutez un bouton avec cet arrière-plan et définissez la position alignParentTop
et alignParentRight
sur true. Et rendre la mise en page parent Relative
.