[英]Android ListView TextSize
我的listview.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>
當前列表視圖中文本的大小很大。 而且我似乎無法弄清楚如何更改文本大小。
真正重要的是您在BaseAdapter
的getView(..)
或CursorAdapter
的newView(..)
返回的行View
。
您可以從Android來源復制simple_list_item_1.xml
或simple_list_item_2.xml
並根據需要進行自定義。
說,改變
android:textAppearance="?android:attr/textAppearanceLarge"
至
android:textAppearance="?android:attr/textAppearanceMedium"
。
另一個選項是通過在TextView
上調用setTextAppearance(context, android.R.attr.textAppearanceMedium)
來更改Adapter
中的文本外觀。
我不確定是否能得到想要的內容,但是listView沒有文本大小,其中的元素是將定義其文本大小的元素。
也就是說,如果將textview添加到列表中,請在textView中定義文本大小。
如果您重命名本地的simple_list_item_1.xml
,Yanchenko的答案非常完美
MyActivity.java
:
ArrayAdapter<String> filesAdapter = new ArrayAdapter<String>(this,
R.layout.simplest_list_item_1, topFilesArray);
filesList.setDivider(null);
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"
在這里僅用於10個列表項。
我認為您使用ListView的方法錯誤。 使用適配器將列表提供給視圖,在ListView布局文件中,您無需定義行,僅定義listview本身和默認textview即可在列表中沒有任何項目時顯示。 然后,您定義一個單獨的xml文件,該文件將呈現每一行。 然后,在此行布局中,您可以隨意設置文本大小。
這是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>
那么您的行布局將是沒有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>
這是適配器的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)
}
...
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.