问题
Is it possible to change the location of the button on the dialog to the outside of the dialog itself? something like this (the red squares are buttons):
I know I can get the button with :
dialog.getButton(AlertDialog.BUTTON_NEGATIVE)
but I couldn't find on the manual the way to change it's location.
回答1:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="20dp"
android:background="#00000000">
<LinearLayout
android:background="@drawable/border_background"
android:layout_gravity="center"
android:gravity="center"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical">
<TextView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@string/update_app"
android:textSize="18sp"
android:textColor="@color/white"
android:layout_gravity="center_horizontal"
android:gravity="center" />
</LinearLayout>
<Button
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_marginTop="20dp"
android:background="#123456"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="14sp"
android:onClick="onUpdateClicked"
android:text="Button" />
Instead of using default alert dialog, make a custom layout something like my layout here. And perform desired action on button.
You can call n show this layout without inflating like this. EDIT:1
public void showUpdateLayout() {
mParentView = (ViewGroup) findViewById(android.R.id.content);
if (mParentView != null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
mUpdateLayout = inflater.inflate(R.layout.upadte_layout, mParentView, false);
mParentView.addView(mUpdateLayout);
if (mUpdateLayout != null) {
mUpdateLayout.setVisibility(View.VISIBLE);
}
}
Write this method in ur public Class (or Custom Aprent Activity). and call this method when u need to alert.
回答2:
you should make custom dialog and set it's root view background color to be transparent: android:background="@android:color/transparent"
回答3:
You will need to create a custom DialogFragment. Below I will give an analytical example of how to implement one and call it with several parameters each time, so you won't need to repeat code each time you want an Dialog
with different message.
CustomAlertDialog.java
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Custom DialogFragment class
*/
public class CustomAlertDialog extends DialogFragment implements
View.OnClickListener {
/**
* Interface for receiving the wanted callbacks
* */
public interface CallbacksListener
{
public void onPositiveButtonClicked();
public void onNegativeButtonClicked();
}
private CallbacksListener callbacksListener;
public void setCallbacksListener(CallbacksListener callbacksListener)
{
this.callbacksListener = callbacksListener;
}
public CustomAlertDialog()
{
//empty constructor
}
private String titleString;
private String messageString;
private String positiveString;
private String negativeString;
@Override
public void setArguments(Bundle bundle)
{
titleString = bundle.getString("titleString");
messageString = bundle.getString("messageString");
positiveString = bundle.getString("positiveString");
negativeString = bundle.getString("negativeString");
}
public static CustomAlertDialog newInstance(AlertDialogStrings alertDialogStrings)
{
CustomAlertDialog customAlertDialog = new CustomAlertDialog();
Bundle b = new Bundle();
b.putString("titleString", alertDialogStrings.titleString);
b.putString("messageString", alertDialogStrings.messageString);
b.putString("negativeString", alertDialogStrings.negativeString);
b.putString("positiveString", alertDialogStrings.positiveString);
customAlertDialog.setArguments(b);
return customAlertDialog;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View v = getActivity().getLayoutInflater().inflate(R.layout.custom_alert_dialog, null);
TextView titleTV = (TextView) v.findViewById(R.id.title_customAlertDialog);
TextView messageTV = (TextView) v.findViewById(R.id.message_customAlertDialog);
Button positiveButton = (Button) v.findViewById(R.id.okBtn_customAlertDialog);
Button negativeButton = (Button) v.findViewById(R.id.cancelBtn_customAlertDialog);
titleTV.setText(titleString);
messageTV.setText(messageString);
positiveButton.setText(positiveString);
negativeButton.setText(negativeString);
positiveButton.setOnClickListener(this);
negativeButton.setOnClickListener(this);
builder.setView(v);
return builder.create();
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.okBtn_customAlertDialog:
callbacksListener.onPositiveButtonClicked();
dismiss();
break;
case R.id.cancelBtn_customAlertDialog:
callbacksListener.onNegativeButtonClicked();
dismiss();
break;
default:
break;
}
}
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
try
{
callbacksListener = (CallbacksListener) activity;
}
catch (ClassCastException e)
{
throw new ClassCastException(activity.toString()
+ " must implement CallbacksListener");
}
}
@Override
public void onDetach()
{
super.onDetach();
callbacksListener = null;
}
/**
* Class for saving the wanted Strings we want to have on our CustomDialog implementation
* */
public static class AlertDialogStrings
{
public String titleString;
public String messageString;
public String positiveString;
public String negativeString;
public AlertDialogStrings(String title, String message, String positiveString, String negativeString)
{
this.messageString = message;
this.titleString = title;
this.positiveString = positiveString;
this.negativeString = negativeString;
}
}
}
custom_alert_dialog.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="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="22sp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:text="My Title Here"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:id="@+id/title_customAlertDialog"/>
<TextView
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="7dp"
android:id="@+id/message_customAlertDialog"
android:layout_below="@id/title_customAlertDialog"
android:textColor="@color/darkGray"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_marginTop="7dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:measureWithLargestChild="true"
android:layout_below="@+id/message_customAlertDialog"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<Button
android:layout_height="wrap_content"
style="?android:attr/buttonBarButtonStyle"
android:textSize="13sp"
android:textColor="@color/primaryColorDark"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:text="@string/cancel"
android:id="@+id/cancelBtn_customAlertDialog"/>
<Button
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:layout_marginRight="10dp"
android:textSize="13sp"
android:textColor="@color/primaryColorDark"
android:layout_height="wrap_content"
style="?android:attr/buttonBarButtonStyle"
android:text="@string/ok"
android:id="@+id/okBtn_customAlertDialog"/>
</LinearLayout>
To show your customAlertDialog:
private void popUpAlertDialog()
{
String title = "My title here?";
String message = "My Message here";
String positiveString = "OK";
String negativeString = "Cancel";
CustomAlertDialog.AlertDialogStrings customDialogStrings =
new CustomAlertDialog.AlertDialogStrings
(title, message, positiveString, negativeString);
CustomAlertDialog customAlertDialog =
CustomAlertDialog.newInstance(alertDialogStrings);
customAlertDialog.show(getSupportFragmentManager(), "customAlertDialog");
customAlertDialog.setCallbacksListener(new CustomAlertDialog.CallbacksListener()
{
@Override
public void onPositiveButtonClicked()
{
//do something
}
@Override
public void onNegativeButtonClicked()
{
//do something
}
});
}
The AlertDialogStrings
class helps us maintain our wanted strings in a way that we can re-use our class with different strings each time and the CallbacksListener
helps as settle the way of the OnClick
responds each time. Note that this design follows the Material Design Dialog style principles.
回答4:
Yes. All you have to do is create a custom dialog layout.
In order to achieve that you would create a LinearLayout
with Transparent BG color and inside it you can do whatever is it that you want.
A quick example:
<LinearLayout android:orientation="vertical"
android:layout_width="300"
android:layout_height="500"
android:background="@android:color/transparent">
<LinearLayout android:orientation="vertical"
android:layout_width="270"
android:layout_height="200">
... your content here
</LinearLayout>
<Button android:layout_width="270"
android:layout_height="200"
android:margin_top="10"/>
</LinearLayout>
If you are using a builder to create your Dialog
, you would go for this:
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.your_dialog_layout, null))
...otherwise
Dialog newDialog = new Dialog();
newDialog.setContentView(R.layout.your_dialog_layout);
来源:https://stackoverflow.com/questions/29942141/android-change-dialog-button-location