2016-04-22 5 views
0

Как создать диалоговое окно пользовательского в андроиде, как это, enter image description hereКак создать пользовательское диалоговое окно с двумя кнопками в Android?

Я хочу так же, как это. Как сгенерировать его. пожалуйста, дайте мне предложение.

Я использовал ниже код для диалогового окна, в чем проблема в моем коде? Я не идентифицировал его. пожалуйста, поделитесь мной с идеей.

<?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"> 

    <RelativeLayout 
     android:id="@+id/rl_quit_learning" 
     android:layout_width="match_parent" 
     android:layout_height="150dp" 
     android:background="@drawable/btn_white" 
     android:paddingBottom="@dimen/thirty_dp" 
     android:paddingLeft="@dimen/ten_dp" 
     android:paddingRight="@dimen/ten_dp" 
     android:paddingTop="@dimen/ten_dp"> 

     <TextView 
      android:id="@+id/tv_quit_learning" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:layout_margin="@dimen/ten_dp" 
      android:text="Quit LEarning?" 
      android:textSize="@dimen/twenty_sp" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/tv_description" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/tv_quit_learning" 
      android:layout_centerHorizontal="true" 
      android:layout_marginLeft="@dimen/ten_dp" 
      android:layout_marginRight="@dimen/ten_dp" 
      android:layout_marginTop="@dimen/twenty_dp" 
      android:gravity="center" 
      android:text="You are 400pts. away from \n unlocking rewards. Quit LEarning?" 
      android:textSize="@dimen/sixteen_sp" /> 
    </RelativeLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="125dp" 
     android:gravity="center" 
     android:orientation="horizontal"> 

     <Button 
      android:id="@+id/btn_cancel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Cancel" /> 

     <Button 
      android:id="@+id/btn_video" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Video" /> 

    </LinearLayout> 
</RelativeLayout> 

Пожалуйста, поделитесь с нами идеей. Спасибо.

+1

Вы можете получить ответ на свой вопрос здесь ... http://stackoverflow.com/questions/13341560/how -to-create-a-custom-dialog-box-in-android –

+0

Просто создайте класс, который расширяет 'AlertDIalog.Builder' и раздувает ваш макет, а затем добавляйте его в класс' setVIew (yourView) ' –

+0

Но Отмена и Кнопка видеоигры, которая отображает середину в основном макете. Пожалуйста, см. Изображение диалогового окна – Reena

ответ

2

custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="80dp" 
android:background="#3E80B4" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/txt_dia" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_margin="10dp" 
    android:text="Do you realy want to exit ?" 
    android:textColor="@android:color/white" 
    android:textSize="15dp" 
    android:textStyle="bold"/> 


<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:background="#3E80B4" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/btn_yes" 
     android:layout_width="100dp" 
     android:layout_height="30dp" 
     android:background="@android:color/white" 
     android:clickable="true" 
     android:text="Yes" 
     android:textColor="#5DBCD2" 
     android:textStyle="bold" /> 

    <Button 
     android:id="@+id/btn_no" 
     android:layout_width="100dp" 
     android:layout_height="30dp" 
     android:layout_marginLeft="5dp" 
     android:background="@android:color/white" 
     android:clickable="true" 
     android:text="No" 
     android:textColor="#5DBCD2" 
     android:textStyle="bold" /> 
</LinearLayout> 

Вы можете изменить кнопку с помощью

android:[email protected]/image 

Вы должны расширяет диалог и реализует OnClickListener

public class CustomDialogClass extends Dialog implements 
android.view.View.OnClickListener { 



public Activity c; 
    public Dialog d; 
    public Button yes, no; 

    public CustomDialogClass(Activity a) { 
    super(a); 
    // TODO Auto-generated constructor stub 
    this.c = a; 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.custom_dialog); 
    yes = (Button) findViewById(R.id.btn_yes); 
    no = (Button) findViewById(R.id.btn_no); 
    yes.setOnClickListener(this); 
    no.setOnClickListener(this); 

    } 

    @Override 
    public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.btn_yes: 
     c.finish(); 
     break; 
    case R.id.btn_no: 
     dismiss(); 
     break; 
    default: 
     break; 
    } 
    dismiss(); 
    } 
} 

вызовов Dialog

CustomDialogClass cdd=new CustomDialogClass(Activity.this); 
cdd.show(); 
+0

Но вы можете увидеть мое изображение для диалогового окна. который представляет собой кнопку отображения середины компоновки – Reena

0

Внутри вашего dailog.xml

<?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/white"> 

    <RelativeLayout 
     android:id="@+id/rl_quit_learning" 
     android:layout_width="match_parent" 
     android:layout_height="150dp" 
     android:background="@drawable/btn_white" 
     android:paddingBottom="@dimen/thirty_dp" 
     android:paddingLeft="@dimen/ten_dp" 
     android:paddingRight="@dimen/ten_dp" 
     android:paddingTop="@dimen/ten_dp"> 

     <TextView 
      android:id="@+id/tv_quit_learning" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:layout_margin="@dimen/ten_dp" 
      android:text="Quit LEarning?" 
      android:textSize="@dimen/twenty_sp" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/tv_description" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/tv_quit_learning" 
      android:layout_centerHorizontal="true" 
      android:layout_marginLeft="@dimen/ten_dp" 
      android:layout_marginRight="@dimen/ten_dp" 
      android:layout_marginTop="@dimen/twenty_dp" 
      android:gravity="center" 
      android:text="You are 400pts. away from \n unlocking rewards. Quit LEarning?" 
      android:textSize="@dimen/sixteen_sp" /> 
    </RelativeLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="125dp" 
     android:gravity="center" 
     android:orientation="horizontal"> 

     <Button 
      android:id="@+id/btn_cancel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@mipmap/ic_launcher" /> 

     <Button 
      android:id="@+id/btn_video" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@mipmap/ic_launcher" /> 

    </LinearLayout> 
</RelativeLayout> 

открытым dimens.xml и добавить код, упомянутый ниже

<dimen name="thirty_dp">30dp</dimen> 
<dimen name="ten_dp">10dp</dimen> 
<dimen name="twenty_sp">20sp</dimen> 
<dimen name="twenty_dp">20dp</dimen> 
<dimen name="sixteen_sp">16sp</dimen> 

открытой вытяжке и создать btn_white .Икс мл добавить код, указанный удар

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <corners android:radius="2dp"></corners> 
    <solid android:color="@android:color/white"></solid> 
</shape> 

открытый mainactivity.java и добавить код указанный ниже

final Dialog dialog = new Dialog(MainActivity.this); 
// Include dialog.xml file 
dialog.setContentView(R.layout.dailog); 
dialog.show(); 

Button declineButton = (Button) dialog.findViewById(R.id.btn_cancel); 
// if decline button is clicked, close the custom dialog 
declineButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     // Close dialog 
     dialog.dismiss(); 
    } 
}); 

Button videoButton = (Button) dialog.findViewById(R.id.btn_video); 
// if decline button is clicked, close the custom dialog 
videoButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 

    } 
}); 
Смежные вопросы