2012-10-04 57 views
0

我感興趣的是如何更改列表中文本的大小,然後將其添加到ListView。我正在使用自定義佈局的list_item。問題在於如何在文本進入ListView之前設置文本的大小。我希望能爲你提供幫助。如何以編程方式將列表項中的字體大小添加到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; 
} 

回答

4

只要做到這一點的AdaptergetView()方法,設置font大小。

tv.setTextSize(20.0f);

+0

也就是說,我需要創建自己的適配器 - 繼承標準適配器的版本並重寫getView( )? –

+0

我不知道如何編寫代碼(你可以寫一個樣本嗎? –

+1

@SirukViktor看到這個鏈接http://www.ezzylearning.com/tutorial.aspx?tid=1763429 –

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 
+0

)))不依賴於測試的大小,他應該屏幕分辨率 - 所以我不得不以編程方式更改它 –

1

這裏沒有「文字大小」 你可以改變的TextView裏面的文字大小: 地址:

android:textSize="16sp" 

對XML 或者你可以在getView()方法的適配器通過代碼做到這一點:

YOUR_TEXTVIEW.setTextSize(16); 
-1

試試這個

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

對不起 - 但這不起作用 - 因爲你需要傳遞id的適配器,而不是對象。 –

0

我知道這可能是有點晚了,但我注意到,每個人都忘了他們的答案的東西。 setTextSize方法需要2個參數才能輸入。第二個如果一個浮點數是你想要的大小的數量。首先是一個常量來的TypedValue讓指定什麼裝置的數量,即:像素,DIP,SP,等等......所以對於setTextSize設置爲18SP它會是這個樣子:

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

,你會怎麼做它在ArrayAdapter的getView中。希望這可以幫助那些正在尋找答案並且偶然發現這個問題的其他人。

相關問題