1

I am writing aplication for android 2.3

in main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 
xmlns:android        = "http://schemas.android.com/apk/res/android"
android:orientation  = "vertical"
android:layout_width = "fill_parent"
android:layout_height= "fill_parent" >

    <ListView
    android:id                 =   "@android:id/list"
    android:textSize           =  "4sp"
    android:layout_width       = "fill_parent"
    android:layout_height      = "fill_parent"
    android:drawSelectorOnTop  = "false" />

</LinearLayout>

in ViewHolder.java

public class ViewHolder {
    ImageView icon = null;

    ViewHolder(View base){
        this.icon = (ImageView)base.findViewById(R.id.pic);
    }
}

in ViewHolderDemo.java

public class ViewHolderDemo extends ListActivity {
    private static final String[] items = {"lorem", "ipsum", "dolor", "sit", "amet","consectetuer", "adipiscing", "elit"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setListAdapter(new IconicAdapter());
    }

    public void OnListItemClick(ListView parent, View v, int position, long id){
    }   

    class IconicAdapter extends ArrayAdapter<String> {

        public IconicAdapter() {
            super(ViewHolderDemo.this, R.layout.row, R.id.label, items);
        }

        public View getView(int position, View convertView, ViewGroup parent){  
            View row = super.getView(position, convertView, parent);
            ViewHolder holder = (ViewHolder)row.getTag();

            if(holder == null){         
                holder = new ViewHolder(row);
                row.setTag(holder);

            }
            holder.icon.setImageResource(R.drawable.star);
            return(row);
        }       
    }
}

problem that tired me is : when i changeandroid:textSize in .xml. size of text dont change
may be this is a bug in android
enter image description here
so how i can chage size of text(make small) that exist in ListView?

4

3 回答 3

2
android:textSize = "4sp"

android:textSize is a TextView attribute, not for ListView.

You can override the default textSize by setting the textSize in your app theme. The better way would be to either:

  1. Set it in code (get a reference to the TextView and use setTextSize)
  2. Inflating your own row layout with android:textSize ="4sp" for the TextView

Why this is better than setting it in the app theme? Since the textSize attribute would be shared by all widgets that extend from TextView. So anything like Button or EditText would have that size as well (of course assuming that is not the desired effect).

于 2013-04-17T19:30:10.567 回答
1

You can do so with a custom row XML and BaseAdapter. Not sure if there's an easier way or not, but I know this works.

于 2013-04-17T19:21:31.193 回答
1
  private static final String[] items = {"lorem", "ipsum", "dolor", "sit", "amet","consectetuer", "adipiscing", "elit"} 

I assume you are inflating a custom layout with imageview and textview.

In your getView()

 SpannableString ss2=  new SpannableString(items[position]);
 ss2.setSpan(new RelativeSizeSpan(2f), 0, ss2.length(), 0);  //change the size
 viewHolder.tv.setText(ss2); //set spannable string to your text
于 2013-04-17T19:30:09.047 回答