Reducing font size of AlertDialog.Builder's components

前端 未结 4 589
不思量自难忘°
不思量自难忘° 2020-12-10 03:11

I created an AlertDialogue using the following code :

 int selectedModeId=0;
 public void sortTypeModeSelection(){

    AlertDialog.Builder aler         


        
相关标签:
4条回答
  • 2020-12-10 03:45

    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.

    0 讨论(0)
  • 2020-12-10 03:58

    you have to use custom layout for this.

    enter image description here

    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>
    
    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2020-12-10 04:05

    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

    0 讨论(0)
提交回复
热议问题