开发者

How to change textsize(fontsize) in customlistview?

开发者 https://www.devze.com 2023-03-07 03:55 出处:网络
I tried to change textSize in my app. I want to change my customlistview开发者_开发技巧\'s textSize.

I tried to change textSize in my app. I want to change my customlistview开发者_开发技巧's textSize. My listview's row XML file has image, 3 textView. I want to change the textView's textSize when User click Optionmenu, and click AlertDialog's SingleChoiceItems checkbox.

here is my code.

Do I need to change XMl file?.. Then How can I set my customAdapter?.. Do I need to change my adapter's getView method?

I'm Open to any answer.

public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){

        case R.id.ks_notice_menu_textsize:

            final CharSequence[] items = {"normal", "big", "bigger"};
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("select textSize");
            builder.setSingleChoiceItems(items, mSelect, 
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    mSelect = which;
                }
            });

            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {



                    int textSize = (int)Username.getTextSize();  
                    switch(mSelect){
                    case 0:     

                        //here I need to change textSize

                        break;
                    case 1:
                        //here I need to change textSize
                        break;                          


                    case 2:
                        //here I need to change textSize
                        break;
                    }



                }
            });
            builder.setNegativeButton("cancel", null);
            builder.show();
            return true;    
    }
    return false;


In your XML for the ListView's row, you need to add IDs to the TextViews in question. Then, you need to replace:

//here I need to change textSize

with (assuming you want a text size of 5):

customAdapter.setTextSize(5);

And finally, in your customAdapter you need to implement a setTextSize(int) method that retrieves all the views and sets their text sizes to the value passed. It also needs to store that value and use it for any new views created when the user scrolls further down.

Something like:

public void setTextSize(final int textSize) {
    fTextSize = textSize;

    for (View view : fViews) {
        ((TextView) view.findViewById(R.id.list_view_text_1).setTextSize(textSize);
        ((TextView) view.findViewById(R.id.list_view_text_2).setTextSize(textSize);
        ((TextView) view.findViewById(R.id.list_view_text_3).setTextSize(textSize);
    }
}

and:

public View getView(final int position, final View convertView, final ViewGroup parent) {
    View view = // retrieve your view from XML.
    fViews.add(view);
    ((TextView) view.findViewById(R.id.list_view_text_1).setTextSize(textSize);
    ((TextView) view.findViewById(R.id.list_view_text_2).setTextSize(textSize);
    ((TextView) view.findViewById(R.id.list_view_text_3).setTextSize(textSize);

    return (view);
}
0

精彩评论

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