java 如何在将列表项添加到 ListView 之前以编程方式设置列表项中的字体大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12734322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:00:57  来源:igfitidea点击:

How to programmatically set the size of the font in the list item before adding it to the ListView?

javaandroidlistviewtext-size

提问by Siruk Viktor

I'm interested in how you can change the size of the text in the list and then add it to the ListView. I'm using a list_item with custom layout . The question is how you can set the size of the text before it gets into the ListView. I hope for your help.

我对如何更改列表中文本的大小然后将其添加到 ListView 感兴趣。我正在使用带有自定义布局的 list_item 。问题是如何在文本进入 ListView 之前设置文本的大小。我希望得到你的帮助。

list_item.xml

列表项.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:android1="http://schemas.android.com/apk/res/android">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

    <TextView
        android:id="@+id/shop_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="1"
        />

    <TextView
        android:id="@+id/shop_address"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="2"
        />

</LinearLayout>

</LinearLayout>

day_list_events.xml

day_list_events.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:id="@+id/layout"
    >

<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:id="@+id/day_and_data"
    android:text="Перел?к кл??нт?в на сьогодн?шн?й день."
    android:textStyle="bold"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:id="@+id/help_info"
    android:text="(Щоб отримати ?нформац?ю про кл??нта та оформити замовлення - натисн?ть на в?дпов?дного кл??нта у списку)"
    android:textColor="#A0A0A0"
    />

<ListView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/day_list_event"
    >

</ListView>

</LinearLayout>

DayListEventsActivity.java

DayListEventsActivity.java

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Window;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class DayListEventsActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.day_list_events);
    sharedPreferences = getSharedPreferences("impInfo", Context.MODE_PRIVATE);
    Resources res = this.getResources();
    ArrayList<HashMap<String, String>> dayShopsInfo = new ArrayList<HashMap<String, String>>();
    String[] shopName = res.getStringArray(R.array.shop_name);
    String[] shopAddress = res.getStringArray(R.array.shop_address);        
    for(int i = 0; i < shopName.length; i++){
        HashMap<String, String> dayShopsInfoCont = new HashMap<String, String>();
        dayShopsInfoCont.put(KEY_SHOP_NAME, shopName[i]);
        dayShopsInfoCont.put(KEY_SHOP_ADDRESS, shopAddress[i]);
        System.out.println(i+" "+shopName[i]+" "+shopAddress[i]);
        dayShopsInfo.add(dayShopsInfoCont);         
    }
    ListView shopList = (ListView)findViewById(R.id.day_list_event);
    SimpleAdapter shopListAdapter = new SimpleAdapter(this,
            dayShopsInfo,
            R.layout.list_item,
            new String[]{KEY_SHOP_NAME, KEY_SHOP_ADDRESS},
            new int[]{R.id.shop_name, R.id.shop_address});
    shopList.setAdapter(shopListAdapter);
    shopList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}

private final String KEY_SHOP_NAME = "shop name";
private final String KEY_SHOP_ADDRESS = "shop address";
private SharedPreferences sharedPreferences;
}

UPDATE (Solution).

更新(解决方案)。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class DayListEventsActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.day_list_events);
    sharedPreferences = getSharedPreferences("impInfo", Context.MODE_PRIVATE);
    Resources res = this.getResources();
    ArrayList<HashMap<String, String>> dayShopsInfo = new ArrayList<HashMap<String, String>>();
    String[] shopName = res.getStringArray(R.array.shop_name);
    String[] shopAddress = res.getStringArray(R.array.shop_address);        
    for(int i = 0; i < shopName.length; i++){
        HashMap<String, String> dayShopsInfoCont = new HashMap<String, String>();
        dayShopsInfoCont.put(KEY_SHOP_NAME, shopName[i]);
        dayShopsInfoCont.put(KEY_SHOP_ADDRESS, shopAddress[i]);
        System.out.println(i+" "+shopName[i]+" "+shopAddress[i]);
        dayShopsInfo.add(dayShopsInfoCont);         
    }
    ListView shopList = (ListView)findViewById(R.id.day_list_event);
    MySimpleAdapter shopListAdapter = new MySimpleAdapter(this,
            dayShopsInfo,
            R.layout.list_item,
            new String[]{KEY_SHOP_NAME, KEY_SHOP_ADDRESS},
            new int[]{R.id.shop_name, R.id.shop_address});
    shopList.setAdapter(shopListAdapter);
    shopList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}

public class MySimpleAdapter extends SimpleAdapter{

    public MySimpleAdapter(Context context,
                            List<? extends Map<String, ?>> data,
                            int resource,
                            String[] from, int[] to) {
        super(context, data, resource, from, to);           
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View returnView = super.getView(position, convertView, parent);
        TextView shopName = (TextView)returnView.findViewById(R.id.shop_name);
        shopName.setTextSize(20.0f);
        return returnView;
    }

}

private final String KEY_SHOP_NAME = "shop name";
private final String KEY_SHOP_ADDRESS = "shop address";
private SharedPreferences sharedPreferences;
}

回答by Kumar Vivek Mitra

Simply do this in the getView()method of the Adapter, to set the fontsize.

只需在 , 的getView()方法中执行此操作Adapter即可设置font大小。

tv.setTextSize(20.0f);

tv.setTextSize(20.0f);

回答by Piotr Chojnacki

If you are using xml for setting layout for list_itemand there you have TextViewyou can change the size of the text like this:

如果您使用 xml 来设置布局,list_item那么TextView您可以像这样更改文本的大小:

<TextView  
    android:id="@+id/text_id"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="20sp" /> // This is a size of your text

回答by Givi

There's not "size of text" You can change the size of the text inside the TextView: Add:

没有“文本大小”您可以更改 TextView 内文本的大小:添加:

 android:textSize="16sp" 

TO your XML Or you can do it by code in the getView() method in the Adapter:

到你的 XML 或者你可以通过 Adapter 中的 getView() 方法中的代码来完成:

YOUR_TEXTVIEW.setTextSize(16);

回答by Mark Dail

I know this might be a little late, however I noticed that everyone forgot something in their answers. the method setTextSize takes 2 parameters for it's input. The second if a float that is the number of the size you want. The first is a TypedValue Constant to let specify what the number means ie: pixels, dip, sp, etc... so for the setTextSize set to 18sp it would look something like this:

我知道这可能有点晚了,但是我注意到每个人都忘记了他们的答案。方法 setTextSize 需要 2 个参数作为输入。第二个如果是您想要的大小的数字的浮点数。第一个是 TypedValue 常量,用于指定数字的含义,即:像素、倾角、sp 等...因此对于设置为 18sp 的 setTextSize,它看起来像这样:

TextView yourTextView = (TextView)findViewById(R.id.yourTextViewId);
yourTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP,18)

and you would do it in the getView of the ArrayAdapter. Hope this helps someone else who was looking for an answer and stumble upon this one.

并且您将在 ArrayAdapter 的 getView 中执行此操作。希望这可以帮助其他正在寻找答案并偶然发现这个答案的人。

回答by Nirav Ranpara

Try this one

试试这个

TextView textView = (TextView) findViewById (R.id.yourTextViewId);
textView.setTextSize(18);