如何更改列表视图的文本大小 [英] how to change text size of listview

查看:57
本文介绍了如何更改列表视图的文本大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用列表活动从SQLITE检索数据.但是我无法设置列表视图的字体大小.请帮助我.

I am using List Activity to retrieve data from SQLITE. But I can not set the font size of list view. Please help me.

public class CartList extends ListActivity {
    private ArrayList<String> results = new ArrayList<String>();
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(com.example.easyshopping.R.layout.cart);
        openAndQueryDatabase();
        displayResultList();
    }
    private void displayResultList() {
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, results));
        getListView().setTextFilterEnabled(true);   }
        private void openAndQueryDatabase() {
        try {
            SQLiteDatabase database = openOrCreateDatabase("ORCL", MODE_PRIVATE, null);
            Cursor c = database.rawQuery("SELECT title,qty,price FROM CART;", null);
                         if (c != null ) {
                int totalPrice=0;
                if  (c.moveToFirst()) {
                    do {
                        String title = c.getString(c.getColumnIndex("title"));
                        int qty = c.getInt(c.getColumnIndex("qty"));
                        int price = c.getInt(c.getColumnIndex("price"));
                        int pricePerTitle=price*qty;
                        results.add("Title: " + title + ",  Quantity: " + qty+",  Price: $"+pricePerTitle);
                        totalPrice=totalPrice+pricePerTitle;
                    }while (c.moveToNext());
                }
               TextView tTotalPrice=(TextView)findViewById(com.example.easyshopping.R.id.txttotalprice);
                String total= Integer.toString(totalPrice);
               tTotalPrice.setText(total);
            }
        } catch (SQLiteException se ) {
            Log.e(getClass().getSimpleName(), "Could not create or Open the database");
        }

    }

cart.xml

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" android:layout_gravity="center">
        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/shoppinglist"

                ></ImageView>

            </RelativeLayout>

    <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@android:id/list"
            ></ListView>

    <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" android:layout_gravity="center" >
       <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="Total Price is:$ "
               android:textColor="#f3f607"
               android:textSize="10dp"
               android:id="@+id/txttotal"
               ></TextView>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#f3f607"
                android:textSize="10dp"
                android:layout_toRightOf="@+id/txttotal"
               android:id="@+id/txttotalprice"></TextView>
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="send your Shopping List."
                android:textSize="12dp"
                android:id="@+id/cartButton"
                android:layout_below="@+id/txttotal"></Button>

            </RelativeLayout>

在列表视图中,字体大小很大.

the font size is very big in list view.

推荐答案

进入布局文件夹并创建一个名为mytextview.xml(无论您如何命名)的新xml文件

Go into layout folder and create a new xml file named mytextview.xml(whatever you want to name it)

    <?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"
        android:textSize="2dp" /> 

和代码中

    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, results));

将其更改为

    setListAdapter(new ArrayAdapter<String>(this,
            R.layout.mytextview, results));

这篇关于如何更改列表视图的文本大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆