开发者

android-How to set the text size of list item in alert dialog

In My application one button is there when you click on that one alert dialog will be appear. that alert dialog consists of single choice list items. Here i want to set the text size of single c开发者_开发问答hoice list item. is it possible? if yes how to do it.

The following is my code

sclist.java

package com.examples.scl;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class sclist extends Activity {

 private static final int DIALOG_SINGLE_CHOICE = 1;


  @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_SINGLE_CHOICE:
            return new AlertDialog.Builder(sclist.this)
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle("Single choice list")
                .setSingleChoiceItems(R.array.select_dialog_items2, 0, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked on a radio button do some stuff */
                    }
                })
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked Yes so do some stuff */
                    }
                })
                .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked No so do some stuff */
                    }
                })
               .create();
        }
        return null;
        }

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    /* Display a radio button group */
    Button radioButton = (Button) findViewById(R.id.radio_button);
    radioButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            showDialog(DIALOG_SINGLE_CHOICE);
        }
    });
}
}


I just encountered this problem myself on a matching game I'm working on. My solution isn't simple but I wanted to use a custom font, and I didn't see an easy way to do it with the 2.2 Android interface (which is what I'm targeting). The trick is to attach an OnShowListener to the alert dialog before you show it. In that listener, get ListAdapter out of the ListView and wrap it with a proxy object that forwards all the calls except the getView. In that function, cast the View to a TextView, set the typeface and size, and return the view. Here's my code:

                    // Add your list with builder up here
        AlertDialog alert = builder.create();
        alert.setOnShowListener(new OnShowListener() {

            @Override
            public void onShow(DialogInterface alert) {
                ListView listView = ((AlertDialog)alert).getListView();
                final ListAdapter originalAdapter = listView.getAdapter();

                listView.setAdapter(new ListAdapter()
                {

                    @Override
                    public int getCount() {
                        return originalAdapter.getCount();
                    }

                    @Override
                    public Object getItem(int id) {
                        return originalAdapter.getItem(id);
                    }

                    @Override
                    public long getItemId(int id) {
                        return originalAdapter.getItemId(id);
                    }

                    @Override
                    public int getItemViewType(int id) {
                        return originalAdapter.getItemViewType(id);
                    }

                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        View view = originalAdapter.getView(position, convertView, parent);
                        TextView textView = (TextView)view;
                        textView.setTypeface(MyFontUtil.getTypeface(MyActivity,MY_DEFAULT_FONT));
                        textView.setTextColor(Color.BLACK);
                        textView.setTextSize(25); // FIXIT - absolute size 
                        return view;
                    }

                    @Override
                    public int getViewTypeCount() {
                        return originalAdapter.getViewTypeCount();
                    }

                    @Override
                    public boolean hasStableIds() {
                        return originalAdapter.hasStableIds();
                    }

                    @Override
                    public boolean isEmpty() {
                        return originalAdapter.isEmpty();
                    }

                    @Override
                    public void registerDataSetObserver(DataSetObserver observer) {
                        originalAdapter.registerDataSetObserver(observer);

                    }

                    @Override
                    public void unregisterDataSetObserver(DataSetObserver observer) {
                        originalAdapter.unregisterDataSetObserver(observer);

                    }

                    @Override
                    public boolean areAllItemsEnabled() {
                        return originalAdapter.areAllItemsEnabled();
                    }

                    @Override
                    public boolean isEnabled(int position) {
                        return originalAdapter.isEnabled(position);
                    }

                });

            }

        });


        alert.show();

If you want to see it in action look on the Android Market in a few weeks. Search for metaphyze (my publisher id). I haven't decided what to call it yet. (It's not "FlashMatch Chinese I Free". That was my first game. This is a kid's matching game. Play the game and tap the picture at the end. You'll see the AlterDialog with the style list.).


Good question. I believe you'd have to use the AlertDialog.Builder constructor that also takes a theme AlertDialog.Builder(Context context, int theme), see if you can see anything useful in that description, I've never tried it myself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜