Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

android - Reducing font size of AlertDialog.Builder's components

I created an AlertDialogue using the following code :

 int selectedModeId=0;
 public void sortTypeModeSelection(){

    AlertDialog.Builder alertBuilder=new AlertDialog.Builder(WatchListDetailActivity.this);

    alertBuilder.setSingleChoiceItems(R.array.watchlist_sorting_modes,selectedModeId, new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
                case 0:
                    selectedModeId=0;
                    break;
                case 1:
                    selectedModeId=1;
                    break;
                case 2:
                    selectedModeId=2;
                    break;
                case 3:
                    selectedModeId=3;
                    break;
                case 4:
                    selectedModeId=4;
                    break;
                case 5:
                    selectedModeId=5;
                    break;
                case 6:
                    selectedModeId=6;
                    break;
                case 7:
                    selectedModeId=7;
                    break;
            }
            dialog.cancel();
        }
    });
    alertBuilder.show();
}

enter image description here

I made the alert, but I want to reduce the font size of the list items of the dialog. How can I do this?

Note: Don't recommend inflating a custom layout to accomplish this, I wish to know if there is another approach.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I was able to achieve this through styles. I added this style to my styles.xml file in the values directory:

<style name="AlertDialogTheme" parent="android:Theme.Dialog">
    <item name="android:textSize">14sp</item>
</style>

Then when creating the AlertDialog, I wrapped the activity context in a ContextThemeWrapper and passed that into the Builder constructor:

ContextThemeWrapper cw = new ContextThemeWrapper( this, R.style.AlertDialogTheme );
AlertDialog.Builder b = new AlertDialog.Builder( cw );

This produced smaller text size for the list items in the dialog.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...