我对如何更改列表中文本的大小,然后将其添加到ListView感兴趣。我正在使用自定义布局的list_item。问题是如何在文本进入ListView之前设置文本的大小。我希望得到你的帮助。
list_item.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
<?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
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;
}
更新(解决方案)。
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;
}
答案 0 :(得分:5)
只需在getView()
的{{1}}方法中执行此操作,即可设置Adapter
大小。
font
答案 1 :(得分:1)
如果您使用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
答案 2 :(得分:1)
没有“文字大小” 您可以更改TextView中文本的大小: 添加:
android:textSize="16sp"
到您的XML 或者您可以通过适配器中的getView()方法中的代码来执行此操作:
YOUR_TEXTVIEW.setTextSize(16);
答案 3 :(得分:0)
我知道这可能有点晚了,但是我注意到每个人都在他们的答案中忘记了一些东西。方法setTextSize为它的输入接受2个参数。第二个if浮点数是你想要的数量。第一个是TypedValue常量,让我们指定数字的含义,即:像素,dip,sp等等......所以对于设置为18sp的setTextSize,它看起来像这样:
TextView yourTextView = (TextView)findViewById(R.id.yourTextViewId);
yourTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP,18)
你可以在ArrayAdapter的getView中完成它。希望这可以帮助那些正在寻找答案并偶然发现这个答案的人。
答案 4 :(得分:-1)
试试这个
TextView textView = (TextView) findViewById (R.id.yourTextViewId);
textView.setTextSize(18);