Android 列表视图文本大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2856642/
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
Android ListView TextSize
提问by zaid
my listview.xml
我的列表视图.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res/zaid.quotes.dlama">
<ListView android:id="@+id/ListView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:layout_marginBottom="50dp" android:paddingTop="50dp"></ListView>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/Back"
android:layout_alignParentBottom="true" android:text="Go Back"></Button>
<LinearLayout android:layout_width="wrap_content"
android:id="@+id/LinearLayout01" android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<com.admob.android.ads.AdView android:id="@+id/ad"
android:layout_width="fill_parent" android:layout_height="wrap_content"
myapp:backgroundColor="#000000" myapp:primaryTextColor="#FFFFFF"
myapp:secondaryTextColor="#CCCCCC" android:layout_alignParentTop="true" />
</LinearLayout>
</RelativeLayout>
the current size of the text in the listview is large. and i cant seem to figure out how to change the text size.
列表视图中文本的当前大小很大。我似乎无法弄清楚如何更改文本大小。
回答by yanchenko
What really matters here is the row View
you return in BaseAdapter
's getView(..)
or CursorAdapter
's newView(..)
.
这里真正重要的是View
您在BaseAdapter
'sgetView(..)
或CursorAdapter
's 中返回的行newView(..)
。
You can copy simple_list_item_1.xml
or simple_list_item_2.xml
from Android sources and customize it to your needs.
Say, changeandroid:textAppearance="?android:attr/textAppearanceLarge"
toandroid:textAppearance="?android:attr/textAppearanceMedium"
.
您可以复制simple_list_item_1.xml
或simple_list_item_2.xml
从 Android 源中复制并根据您的需要对其进行自定义。
说,android:textAppearance="?android:attr/textAppearanceLarge"
改为android:textAppearance="?android:attr/textAppearanceMedium"
。
The other option is to change text appearance right in the Adapter
by calling setTextAppearance(context, android.R.attr.textAppearanceMedium)
on the TextView
.
另一种选择是改变文本外观正确Adapter
调用setTextAppearance(context, android.R.attr.textAppearanceMedium)
的TextView
。
回答by Maragues
I'm not sure if I'm getting what you want, but a listView doesn't have text size, the elements inside it are the ones that will define their text size.
我不确定我是否得到了您想要的东西,但是 listView 没有文本大小,其中的元素将定义其文本大小。
That is, if you add a textview to the list, define the text size in the textView.
也就是说,如果在列表中添加一个 textview,则在 textView 中定义文本大小。
回答by Andriy Boyko
Yanchenko's answer works perfect, if you rename your local simple_list_item_1.xml
Yanchenko 的回答很完美,如果你重命名你的本地 simple_list_item_1.xml
MyActivity.java
:
MyActivity.java
:
ArrayAdapter<String> filesAdapter = new ArrayAdapter<String>(this,
R.layout.simplest_list_item_1, topFilesArray);
filesList.setDivider(null);
simplest_list_item_1.xml
:
simplest_list_item_1.xml
:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="7pt"
android:paddingLeft="6dip" />
layout_height="wrap_content"
is used here for 10 list items only.
layout_height="wrap_content"
此处仅用于 10 个列表项。
回答by Ricardo Villamil
I think you have the wrong approach to using a ListView. Use an adapter to feed the list to the view, and in the ListView layout file, you don't define the rows, you only define the listview itself and a default textview which will show when there are no items in the list. Then you define a separate xml file which will render each row. Then in this row layout you're free to set the text size as you wish.
我认为您使用 ListView 的方法是错误的。使用适配器将列表提供给视图,在 ListView 布局文件中,您不定义行,您只定义列表视图本身和默认文本视图,该文本视图将在列表中没有项目时显示。然后定义一个单独的 xml 文件,它将呈现每一行。然后在此行布局中,您可以随意设置文本大小。
Here's an example of the listview xml (notice the special id's given to the listview and textview, these ARE required):
下面是一个 listview xml 的例子(注意给了 listview 和 textview 的特殊 id,这些是必需的):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:scrollbars="horizontal" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:paddingLeft="8dp" android:paddingRight="8dp">
<ListView android:id="@+id/android:list" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1"
android:drawSelectorOnTop="false" />
<TextView android:id="@+id/android:empty"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:text="No records found" />
</LinearLayout>
Then your row layout would be your file without the listview:
那么你的行布局将是没有列表视图的文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res/zaid.quotes.dlama">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/Back"
android:layout_alignParentBottom="true" android:text="Go Back"></Button>
<LinearLayout android:layout_width="wrap_content"
android:id="@+id/LinearLayout01" android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<com.admob.android.ads.AdView android:id="@+id/ad"
android:layout_width="fill_parent" android:layout_height="wrap_content"
myapp:backgroundColor="#000000" myapp:primaryTextColor="#FFFFFF"
myapp:secondaryTextColor="#CCCCCC" android:layout_alignParentTop="true" />
</LinearLayout>
</RelativeLayout>
And here's a sample of the adapter's getView() method where you will set the row layout file:
这是适配器的 getView() 方法的示例,您将在其中设置行布局文件:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_layout, null);
}
//Now, assuming your adapter has the list of objects to be displayed in 'records':
if(records != null) {
YourRecord r = records.get(position);
if(r != null) {
// Populate your views in your row.xml based on data from your record selected (r)
}
...
}
}