是否可以将对话框上按钮的位置更改为对话框本身的外部?像这样的东西(红色方块是按钮):
我知道我可以通过以下方式获取按钮:
dialog.getButton(AlertDialog.BUTTON_NEGATIVE)
但我在手册上找不到更改位置的方法。
答案 0 :(得分: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" />
不要使用默认警报对话框,而是在此处创建类似于我的布局的自定义布局。并按下按钮执行所需的操作。
您可以调用n显示此布局而不会像这样膨胀。 编辑: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);
}
}
将此方法写入您的公共类(或自定义Aprent活动)。当你需要提醒时调用这个方法。
答案 1 :(得分:1)
您应该custom dialog并将其根视图背景颜色设置为透明:android:background="@android:color/transparent"
答案 2 :(得分:1)
您需要创建自定义DialogFragment。下面我将给出一个分析示例,说明如何实现一个并且每次都使用多个参数调用它,因此每次希望Dialog
具有不同的消息时,您都不需要重复代码。
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>
显示您的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
}
});
}
AlertDialogStrings
类帮助我们维护我们想要的字符串,我们可以每次使用不同的字符串重用我们的类,而CallbacksListener
有助于解决{{1}的方式每次响应。请注意,此设计遵循Material Design Dialog style principles。
答案 3 :(得分:0)
是。 您所要做的就是创建一个自定义对话框布局。
为了达到这个目的,你可以创建一个带有透明BG颜色的test.AfterAnnotatedTest -> afterTest
,在里面你可以做你想做的任何事情。
一个简单的例子:
LinearLayout
如果您使用构建器来创建<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>
,那么您可以这样做:
Dialog
...否则
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.your_dialog_layout, null))