简体   繁体   English

Android:AlertDialog按钮不接受样式

[英]Android: AlertDialog Button not accepting style

I've got a style set up for my Alert Dialog, and the style is being shown on [most] of the dialog without issue, the only problem are the buttons. 我已经为我的警报对话框设置了一个样式,并且样式在[大多数]对话框上显示没有问题,唯一的问题是按钮。

The phone is an HTC Evo running SenseUI, and the AlertDialog buttons continue to be skinned via the SenseUI theme. 手机是运行SenseUI的HTC Evo,AlertDialog按钮继续通过SenseUI主题进行蒙皮。 I have tried changing my application style (rtg_style) to be a child of Theme.Dialog instead of Theme.Light.NoTitleBar, and the buttons for the activities continue to be styled correctly, but the AlertDialogs also continue to be styled inccorrectly. 我已经尝试将我的应用程序样式(rtg_style)更改为Theme.Dialog的子项而不是Theme.Light.NoTitleBar,并且活动的按钮继续正确设置样式,但AlertDialogs也继续被不正确地设置样式。 I'm trying to avoid having to write a completely custom AlertDialog replacement, what else can I do? 我试图避免编写完全自定义的AlertDialog替换,我还能做什么?

styles.xml: styles.xml:

<style name="rtg_style" parent="@android:style/Theme.Light.NoTitleBar">
    <item name="android:windowBackground">@drawable/bluebg</item>
    <item name="android:buttonStyle">@style/rtg_Button</item>
    <item name="android:listViewStyle">@style/rtg_ListView</item>
    <item name="android:expandableListViewStyle">@style/rtg_ExpandableListView</item>
</style>

<style name="rtg_AlertDialog" parent="@style/rtg_style"> <!-- parent="@android:style/Theme.Dialog"> --> 
    <item name="android:buttonStyle">@style/rtg_Button</item>
    <item name="android:listViewStyle">@style/rtg_ListView</item>
    <item name="android:alertDialogStyle">@style/dialog</item>
</style>

<style name="rtg_Button" parent="@android:style/Widget.Button">
    <item name="android:background">@drawable/button</item>
    <item name="android:textColor">#ffffff</item>
    <item name="android:textSize">15sp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:height">40dp</item>
</style>

<style name="rtg_ListView" parent="@android:style/Widget.ListView">
    <item name="android:listSelector">@drawable/listview</item>
</style>

<style name="rtg_ExpandableListView" parent="@android:style/Widget.ExpandableListView">
    <item name="android:listSelector">@drawable/listview</item>
</style>    

<style name="base">
    <item name="android:padding">10dp</item>
</style>    

<style name="title" parent="@style/base">
      <item name="android:textColor">#FFFFFF</item>
    <item name="android:textSize">16sp</item>
    <item name="android:textStyle">bold</item>
</style>

<style name="body" parent="@style/base">
    <item name="android:textColor">#000000</item>
    <item name="android:textStyle">normal</item>
</style>

<style name="dialog">
    <item name="android:fullDark">@drawable/dialog_body</item>
    <item name="android:topDark">@drawable/dialog_title</item>
    <item name="android:centerDark">@drawable/dialog_body</item>
    <item name="android:bottomDark">@drawable/dialog_footer</item>
    <item name="android:fullBright">@drawable/dialog_body</item>
    <item name="android:centerBright">@drawable/dialog_body</item>
    <item name="android:bottomBright">@drawable/dialog_footer</item>
    <item name="android:bottomMedium">@drawable/dialog_footer</item>
    <item name="android:centerMedium">@drawable/dialog_body</item>
    <item name="android:windowIsFloating">true</item>

</style>

Activity.java: Activity.java:

AlertDialog.Builder ab = new AlertDialog.Builder(new ContextThemeWrapper(OrderSummary.this, R.style.rtg_AlertDialog));
            ab.setTitle("Select a reason");
            String[] reasons = new String[Shared.Reasons_RejectAll.size()];
            for (int i = 0; i < Shared.Reasons_RejectAll.size(); i++) { 
                try {
                    reasons[i] = Shared.Reasons_RejectAll.get(i).Name;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            ab.setItems(reasons, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    rejectReason = Shared.Reasons_RejectAll.get(which).Name;
                    for (int i = 0; i <= r.ItemList.length; i++){
                        r.ItemList[index].item.get(i).setStatus(eItemStatus.REJECTED);
                        r.ItemList[index].item.get(i).setRejectReason(rejectReason);
                    }
                    adapter.notifyDataSetChanged();
                }
            }) 
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // No additional code required at this time.
                }
            });
            //ab.show();

            AlertDialog dialog = ab.create();
            dialog.show();

Something I just tried that works. 我试过的东西有效。 You can use getButton(int whichButton) in order to extract the Button's View class and then you can butcher it however you want. 您可以使用getButton(int whichButton)来提取Button的View类,然后您可以根据需要对其进行屠宰。

Before you do this, call the .show() function, otherwise the Button themselves won't be created yet, and the getButton() command gives you null, and NullPointerExceptions suck. 在执行此操作之前,请调用.show()函数,否则将不会创建Button本身,并且getButton()命令将为null,并且NullPointerExceptions很糟糕。 After you get the button, you can do Button/TextView (Button is an extended TextView) functions like .setBackgroundDrawable and .setTextColor. 获得按钮后,您可以执行Button / TextView(Button是扩展TextView)功能,如.setBackgroundDrawable和.setTextColor。

AlertDialog alert = new AlertDialog.Builder(activity).create();
// configure things here like the title and message
alert.show();

Button button = alert.getButton(AlertDialog.BUTTON_NEGATIVE);
button.setBackgroundResource(R.drawable.negative_button_selector);

If someone can figure out how to set a style/theme outside creating a new Button and swiping that button's stuff, I'm all ears because that would be better. 如果有人能够弄清楚如何在创建新按钮之外设置样式/主题并轻扫该按钮的东西,我会全神贯注,因为那会更好。

Rock! 岩石! Rock On! 摇滚!

Found the answer! 找到答案了! From what I've researched, the buttons on an alertdialog use their own layout that cannot be overridden, so I ended up subclassing Dialog and building a custom dialog myself. 根据我的研究,alertdialog上的按钮使用自己的布局,无法覆盖,所以我最终继承了Dialog并自己构建了一个自定义对话框。

CustomDialog.java: CustomDialog.java:

public class CustomDialog extends Dialog implements OnClickListener {
TextView tvTitle;
TextView tvMessage;
Button btnOK;
ListView listView;
Context context;
ProgressBar prgWait;
ProgressBar prgBar;

public CustomDialog(Context context) {
    super(context, R.style.rtg_DialogActivity);
    setContentView(R.layout.dialog);
    this.context = context;

    // replace the background dim with a background blur effect
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.dimAmount = 0.0f;
    getWindow().setAttributes(lp);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

    tvTitle = (TextView) findViewById(R.id.tvDialogTitle);
    tvMessage = (TextView) findViewById(R.id.tvDialogMessage);
    btnOK = (Button) findViewById(R.id.btnDialog);
    listView = (ListView) findViewById(R.id.lvDialogList);
    prgWait = (ProgressBar) findViewById(R.id.prgDialog);
    prgBar = (ProgressBar) findViewById(R.id.prgDialogBar);
}

public CustomDialog setTitle(String text) {
    tvTitle.setText(text);      
    return this;
}

public CustomDialog setMessage(String text) {
    tvMessage.setVisibility(View.VISIBLE);
    tvMessage.setText(text);        
    return this;
}

public CustomDialog setList(List<String> list, OnItemClickListener l) {
    ArrayAdapter<String> aa = new ArrayAdapter<String>(this.getContext(), android.R.layout.simple_list_item_1, list);
    listView.setVisibility(View.VISIBLE);
    listView.setAdapter(aa);
    listView.setOnItemClickListener(l);         
    return this;
}

public CustomDialog setList(String[] list, OnItemClickListener l) {
    List<String> lString = new ArrayList<String>();
    for (String s : list)
        lString.add(s);     
    return setList(lString, l);
}

public CustomDialog setButton(String text, View.OnClickListener l) {
    btnOK.setText(text);
    btnOK.setOnClickListener(l);
    return this;
}

public CustomDialog setButton(String text) {
    return setButton(text, this);
}

public CustomDialog isIndeterminate(boolean b) {
    if (b) {
        prgWait.setVisibility(View.VISIBLE);
        btnOK.setVisibility(View.GONE);
    } else {
        prgWait.setVisibility(View.GONE);
        btnOK.setVisibility(View.VISIBLE);
    }
    return this;
}

public CustomDialog isProgress(boolean b) {
    if (b) {
        prgBar.setVisibility(View.VISIBLE);
        btnOK.setVisibility(View.GONE);
    } else {
        prgBar.setVisibility(View.GONE);
        btnOK.setVisibility(View.VISIBLE);
    }
    return this;
}

public CustomDialog setProgressMax(int max) {
    prgBar.setMax(max);
    return this;
}

public CustomDialog setProgress(int progress) {
    prgBar.setProgress(progress);
    return this;
}

public int getProgressMax() {
    return prgBar.getMax();
}

public int getProgress() {
    return prgBar.getProgress();
}

@Override
public void onClick(View v) {
    this.dismiss();
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM