gpt4 book ai didi

android - 为什么我的两个 AlertDialog 按钮之间没有间隙?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:57:53 25 4
gpt4 key购买 nike

在我的 AlertDialog 中,我的肯定按钮和否定按钮是“附加的”。我很确定他们之间应该有差距。有人能告诉我为什么会这样吗?我很乐意提供任何代码。 Here is what my AlertDialog looks like.

我有一个用于正文的自定义 View 以及我的 AlertDialog 的标题(我不会发布该 XML 代码,因为我认为没有必要,但请告诉我。)在 MainActivity 中,我膨胀我的自定义标题和正文 View ,并覆盖 setPositive() 和 setNegative(),然后我使用 onShow() 自定义按钮的颜色。

对于复杂的代码,我们深表歉意,但我们将不胜感激 :)。这是我的 MainActivity:

public void openPrompt(View view){
//builds and opens custom view with prompt.XML
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
EditText input = (EditText)promptView.findViewById(R.id.userInput);

builder.setCancelable(true).setView(R.layout.customdialoglayout)
.setNegativeButton("One", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"CANCEL clicked",Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("Two", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"SET clicked",Toast.LENGTH_SHORT).show();
}
});


//set title with custom XML layout view
LayoutInflater inflater = getLayoutInflater();
View titleView = inflater.inflate(R.layout.cutomtitlebar,null);
builder.setCustomTitle(titleView);

AlertDialog ad = builder.create();

//change colors of background and buttons
ad.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {

Context context = MainActivity.this;
Window view = ((AlertDialog)dialog).getWindow();

view.setBackgroundDrawableResource(R.color.colorPrompt);
Button negButton = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_NEGATIVE);
negButton.setBackgroundColor(context.getResources().getColor(R.color.colorPromptButton));
negButton.setTextColor(context.getResources().getColor(R.color.colorPromptButtonText));

Button posButton = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE);
posButton.setBackgroundColor(context.getResources().getColor(R.color.colorPromptButton));
posButton.setTextColor(context.getResources().getColor(R.color.colorPromptButtonText));
}
});


ad.show();


}

编辑 这是我用于 setView() 的 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="CUSTOM TEXT"
android:id="@+id/textView3"
android:layout_gravity="center"/>

最佳答案

如果我们需要在您的按钮之间留出间隙,我们为什么不即兴发挥呢?您可以使用中性按钮而不是使用否定按钮。这是因为正面和负面是密不可分的,但是中性和正面在最右侧和最左侧都可以正常工作。所以只需将否定按钮更改为中立即可。见下面的代码:

AlertDialog.Builder builder = new AlertDialog.Builder(preview.this);
builder.setTitle("Title here");
builder.setMessage("message on dialog here");
builder.setCancelable(true);
builder.setNeutralButton("Name of button N", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//your code here
}
});
builder.setPositiveButton("Name of button P", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//your code here
}
});

这是一个非常适合我的示例,您可以根据您的代码对其进行如下编辑:

builder.setCancelable(true).setView(R.layout.customdialoglayout)
.setNeutralButton("One", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"CANCEL clicked",Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("Two", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"SET clicked",Toast.LENGTH_SHORT).show();
}
});

关于android - 为什么我的两个 AlertDialog 按钮之间没有间隙?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37951871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com