2015-07-21 48 views
0

我正在做一个警告对话框的应用程序,我有一个问题,我的alertdialog显示在右侧,如果我把一个负面的两个是正确的我怎么能centerButtonButton?中心PositiveButton

public void showBandDialog(){  
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    LayoutInflater inflater  = LayoutInflater.from(getActivity()); 
    final View view2   = inflater.inflate(R.layout.bluetooth_dialog, null);   
    if(bluetoothIsActive){ 
     ImageView bluetootIndicator = (ImageView) view2.findViewById(R.id.bluetooth_indicator); 
     bluetootIndicator.setImageResource(R.drawable.checked); 
    } 
    if(bluetoothIsSaved){ 
     ImageView bluetootSavedIndicator = (ImageView) view2.findViewById(R.id.bluetooth_saved_indicator); 
     bluetootSavedIndicator.setImageResource(R.drawable.checked); 
    }  
    builder.setTitle(Html.fromHtml("<font color='#55BFE7'>Bluetooth</font>")); 
    builder.setPositiveButton("Done", 
    new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, 
       int which) {} 
    });     
    builder.setView(view2); 
    final AlertDialog alertDialog = builder.create(); 
    alertDialog.setOnShowListener(new OnShowListener() { 
     @Override 
     public void onShow(DialogInterface dialog) { 
      Button okButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); 
      okButton.setOnClickListener(new View.OnClickListener() {      
       @Override 
       public void onClick(View v) { 
        alertDialog.dismiss();                     
       } 
      });    
     } 
    }); 
    alertDialog.show();   
} 
+0

您可以使用自定义布局文件,并根据您的要求放置按钮,只需调用builder.setView(R.layout.dialog_view);并为这些按钮设置OnClickListener –

+1

我认为你不应该改变标准的对齐方式(右对齐的正负按钮) – Booger

+0

您应该阅读对话设计指南https://www.google.com/design/ spec/components/dialogs.html# –

回答

1

你可以使用自定义布局,forexample创建一个额外的活动塔将被调用,当你想展示一些通知。要从此通知活动接收数据,您可以使用startActivityForResult()启动它,并在您收到此消息时执行一些操作。 您的自定义通知活动,你可以在清单使用:

<activity 
     android:name=".NotifiactionActivity" 
     android:theme="@android:style/Theme.Holo.Light.Dialog" > 
    </activity> 

有,你可以用其他的主题,但它看起来像一个警报。

1

您可以随时使用Neutral Button实现正按键相同的效果,而无需创建你的对话框特定的布局文件。

AlertDialog.Builder - Neutral Button

另外:Android: Difference between positive, negative, and neutral button

+0

这是一个很好的选择,可以避免创建自定义布局文件。 – Machado

+0

切换到中性,现在向左移,负向和正向在右侧,有一些对齐可以使用? – GusDev

+0

有趣的是,你使用哪个版本的Android进行测试?@ portal47 –