Change font size in ListView - Android/Eclipse

前端 未结 6 783
鱼传尺愫
鱼传尺愫 2020-12-13 20:28

How can I change the font size in a ListView element? In my main.xml file, I have tried several different values in for android:textSize (pt,px,sp,dp) and nothing seems to c

相关标签:
6条回答
  • 2020-12-13 21:04

    Create another xml file for the Text that should be in the list rows..

    and just add android:layout_height="?android:attr/listPreferredItemHeight" in your TextView :D (put it in a relative layout rather than a linear layout)

    and then use your previous ListView xml for the setContentView(R.layout.listview)

    and the other xml file in your ArrayAdapter code

    ArrayAdapter adapter = new ArrayAdapter(this, R.layout.yoursecondLayoutforthelistswithText, R.id.titles, HistoryList)

    the R.id.titles is the id for the TextView

    0 讨论(0)
  • 2020-12-13 21:05

    Go into layout create a new xml file named mylist.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@android:id/text1"  
            android:paddingTop="2dip" 
            android:paddingBottom="3dip" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" /> 
    

    now in the code

            adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,HistoryList);
    

    change it to

            adapter = new ArrayAdapter<String> (this,R.layout.mylist,HistoryList);
    
    0 讨论(0)
  • 2020-12-13 21:08

    Please create another file named list_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:padding="6dp"
        android:textSize="21sp">
    

    0 讨论(0)
  • 2020-12-13 21:09

    2 ways to go:

    1. Copy simple_list_item_1.xml from Android sources, modify it and then use it instead of android.R.layout.simple_list_item_1;
    2. Use BaseAdapter and modify font size in getView(..) call.

    I'd suggest you go with latter.

    0 讨论(0)
  • 2020-12-13 21:13

    you can save font size in sharedprefs and then recreate your adapter

        SharedPreferences.Editor ed = mSharedPreferences.edit();
        ed.putFloat("fontTwosize", sizeTwo);
        ed.commit();
    
        adapterz = new LazyziyaratAdapter(MainActivity.this,
                        ziyaratList);
    
        listz.setAdapter(adapterz);
    

    and in your getview method of adapter

     holder.text.setTextSize(TypedValue.COMPLEX_UNIT_SP,G.mSharedPreferences.getFloat("fontTwosize", 25));
    
    0 讨论(0)
  • 2020-12-13 21:18

    The quick answer is that you can only change the text size by using your own layout rather than the android.R.layout.simple_list_item one. By doing that you can change the TextView properties such as the text size. However if you haven't worked with lists with your own layouts, they can appear a bit complex to use the first time. I'll try to come back and edit this answer to provide more information later, but I suggest you look for tutorials on custom list views - that will show you how to inflate your own layout files.

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