Java 更改 ListView 中的字体大小 - Android/Eclipse

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2464056/
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-08-13 08:03:46  来源:igfitidea点击:

Change font size in ListView - Android/Eclipse

javaandroideclipselistview

提问by Soren

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 change it.

如何更改 ListView 元素中的字体大小?在我的 main.xml 文件中,我为 android:textSize (pt,px,sp,dp) 尝试了几个不同的值,但似乎没有任何改变。

Here is what I have currently for the in my main.xml:

这是我目前在 main.xml 中的内容:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:textColor="#ffffff"
    android:background="#000080" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:dividerHeight="1px"
    android:layout_marginTop="5px" 
    android:textSize="8px"/>

Here is my Java:

这是我的Java:

package com.SorenWinslow.TriumphHistory;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class TriumphHistory extends ListActivity {
    String[] HistoryList;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ArrayAdapter<String> adapter;
        HistoryList = getResources().getStringArray(R.array.history);
        adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,HistoryList);
        setListAdapter(adapter);
    }

}

采纳答案by yanchenko

2 ways to go:

2种方法:

  1. Copy simple_list_item_1.xmlfrom Android sources, modify it and then use it instead of android.R.layout.simple_list_item_1;
  2. Use BaseAdapterand modify font size in getView(..)call.
  1. simple_list_item_1.xml从 Android 源复制,修改它,然后使用它而不是android.R.layout.simple_list_item_1;
  2. BaseAdaptergetView(..)通话中使用和修改字体大小。

I'd suggest you go with latter.

我建议你选择后者。

回答by Steve Haley

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 inflateyour own layout files.

快速回答是,您只能使用自己的布局而不是 android.R.layout.simple_list_item 来更改文本大小。通过这样做,您可以更改 TextView 属性,例如文本大小。但是,如果您没有使用过具有自己布局的列表,那么第一次使用它们可能会显得有点复杂。我将尝试返回并编辑此答案以稍后提供更多信息,但我建议您查找有关自定义列表视图的教程 - 这将向您展示如何使用inflate您自己的布局文件。

回答by Vasile Surdu

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

为应该在列表行中的文本创建另一个 xml 文件。

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

只需在 TextView 中添加 android:layout_height="?android:attr/listPreferredItemHeight" :D(将其置于相对布局而不是线性布局中)

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

然后将您以前的 ListView xml 用于 setContentView(R.layout.listview)

and the other xml file in your ArrayAdapter code

以及 ArrayAdapter 代码中的另一个 xml 文件

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

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

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

R.id.titles 是 TextView 的 ID

回答by Shereef Marzouk

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

进入布局创建一个名为 mylist.xml 的新 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);

回答by Zou

Please create another file named list_item.xml

请创建另一个名为 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">

回答by Hadid Graphics

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

您可以在 sharedprefs 中保存字体大小,然后重新创建您的适配器

    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

并在适配器的 getview 方法中

 holder.text.setTextSize(TypedValue.COMPLEX_UNIT_SP,G.mSharedPreferences.getFloat("fontTwosize", 25));