Reducing font size of AlertDialog.Builder's components

♀尐吖头ヾ 提交于 2019-11-27 03:16:26

问题


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();
}

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.


回答1:


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.




回答2:


you have to use custom layout for this.

check below code it sure help you

AlertDialog.Builder builder = new AlertDialog.Builder(forgatps.this);
            builder.setTitle("Select Your Account");

            builder.setAdapter(new ArrayAdapter<String>(forgatps.this,
                    R.layout.row_email, R.id.textView1, emails),
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            MainEmail = emails.get(which);
                            editEmail.setText("" + MainEmail);

                        }
                    });

            builder.show();

row file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:background="#ffffff"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#000000"
        android:textSize="26dp" />

</LinearLayout>



回答3:


Try out as below :

Its just a way i am showing you the way its possible.

  final CharSequence[] itemsMark =
   {getResources().getString(R.string.Mark_As_Beautiful),
              getResources().getString(R.string.Mark_As_Fun),
              getResources().getString(R.string.Mark_As_Not_A_Portrait),
              getResources().getString(R.string.Mark_As_Offensive),
              getResources().getString(R.string.Mark_As_Spam),
              getResources().getString(R.string.Mark_As_Cancel)
   };
   ArrayAdapter<CharSequence> itemsAdapter = new
   ArrayAdapter<CharSequence> (this,
            R.layout.menu_items, itemsMark);
   builder = new AlertDialog.Builder(this);
   builder.setTitle("My Title");
   builder.setIcon(R.drawable.icon);
   builder.setAdapter(itemsAdapter, new DialogInterface.OnClickListener()
   {
       public void onClick(DialogInterface dialog, int item) {
                  switch(item) {
                          case 0:
                      //Mark as Beautiful
                          break;
                       case 1:
                      //Mark as Beautiful
                          break;
                    case 2: 
                        //Mark as Not a Portrait
                          break;
                    case 3:
                      //Mark as Offensive
                          break;
                   case 4:
                      //Mark as Spam
                          break;
                   case 5:
                      //cancel
                   break;
           }
       }
   });

And in layout/menu_items.xml :

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@android:id/text1"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:padding="10dip"
       android:layout_margin="5dip"
       android:gravity="center_vertical"
       android:textSize="22dip"
       android:textColor="#ff000000"
       android:typeface="normal"
       android:lineSpacingExtra="0dip"/>

I hope it will help you.

Thanks.




回答4:


I don't see why you would need to change the text size without using a custom layout file like shown in the present answers(unless you're maybe implementing some sort of accessibility option keeping the current platform's look and feel?). Anyway you could still modify the text size by directly modifying the dialog's list. Something like this:

private static int mTextSize = 5;
private static int mFirstVisible = -1;
private static int mLastVisible = -1;

v.post(new Runnable() {

                @Override
                public void run() {
                    changeCurrentVisibleSize(ad.getListView());
                    // ad is the AlertDialog resulted from alertBuilder.create
                    ad.getListView().setOnScrollListener(
                            new OnScrollListener() {

                                @Override
                                public void onScrollStateChanged(
                                        AbsListView view, int scrollState) {
                                    // TODO Auto-generated method stub

                                }

                                @Override
                                public void onScroll(AbsListView view,
                                        int firstVisibleItem,
                                        int visibleItemCount,
                                        int totalItemCount) {
                                    if (visibleItemCount != 0) {
                                        if (mFirstVisible != firstVisibleItem
                                                || mLastVisible != (firstVisibleItem
                                                        + visibleItemCount - 1)) {
                                            updateRow((ListView) view);
                                        }
                                    }
                                }
                            });
                }

            });

where the changeCurrentVisibleSize() and updateRow() method are:

static void updateRow(ListView listView) {
    final int count = listView.getChildCount();
    final View firstRow = listView.getChildAt(0);
    final View secondRow = listView.getChildAt(count - 1);
    if (firstRow instanceof TextView) {
        ((TextView) firstRow).setTextSize(mTextSize);
        ((TextView) secondRow).setTextSize(mTextSize);
    }
}

static void changeCurrentVisibleSize(ListView listView) {
    final int count = listView.getChildCount();
    mFirstVisible = listView.getFirstVisiblePosition();
    mLastVisible = listView.getLastVisiblePosition();
    for (int i = 0; i < count; i++) {
        final View rowView = listView.getChildAt(i);
        if (rowView instanceof TextView) {
            ((TextView) rowView).setTextSize(mTextSize);
        }
    }
}

Increasing the text should work fine, decreasing the text again should work fine(but in this case the row will continue to have a certain height based on the layout file used).



来源:https://stackoverflow.com/questions/14888832/reducing-font-size-of-alertdialog-builders-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!