减少AlertDialog.Builder组件的字体大小

时间:2013-02-15 05:32:15

标签: android android-alertdialog android-custom-view android-fonts

我使用以下代码创建了AlertDialogue

 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

我发出警告,但我想减少对话框列表项的字体大小。我怎么能这样做?

注意:不建议为自定义布局充气以实现此目的,我想知道是否有其他方法。

4 个答案:

答案 0 :(得分:40)

我能够通过风格实现这一目标。我将此样式添加到values目录中的styles.xml文件中:

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

然后在创建AlertDialog时,我将活动上下文包装在ContextThemeWrapper中并将其传递到Builder构造函数中:

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

这为对话框中的列表项产生了较小的文本大小。

enter image description here

答案 1 :(得分:5)

你必须为此使用自定义布局。

enter image description here

检查以下代码,它确实可以帮助您

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

行文件:

<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>

答案 2 :(得分:3)

试试如下:

它只是一种向你展示其可能性的方式。

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

在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"/>

我希望它会对你有所帮助。

感谢。

答案 3 :(得分:3)

我不明白为什么在不使用当前答案中显示的自定义布局文件的情况下需要更改文本大小(除非您可能实现某种可访问性选项以保持当前平台的外观?) 。无论如何,你仍然可以通过直接修改对话框的列表来修改文本大小。像这样:

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

            });

changeCurrentVisibleSize()updateRow()方法是:

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

增加文本应该可以正常工作,再次减少文本应该工作正常(但在这种情况下,行将继续根据使用的布局文件具有一定的高度。)