2016-03-01 85 views
0

這裏是我的XML:如何將自定義ListViews字體更改爲自定義字體?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:background="#E0E0E0" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context="com.ccb.lldm.lldmhimnario.Cantos"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:elevation="25dp" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="#303F9F" 
     app:popupTheme="@style/AppTheme.PopupOverlay"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:textColor="#E0E0E0" 
      android:text="Cantos" 
      android:id="@+id/toolbarCantos" 
      android:textSize="25sp" 
      android:layout_height="wrap_content" /> 

    </android.support.v7.widget.Toolbar> 

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/ListView" 
    android:layout_below="@+id/toolbar" 
    android:layout_alignParentBottom="true"> 

</ListView> 


</RelativeLayout> 

這是我的列表視圖。

這裏是我的自定義列表視圖的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:background="#E0E0E0" 
android:layout_height="match_parent"> 

<TextView 
    android:layout_width="wrap_content" 
    android:text="Canto: " 
    android:background="@drawable/text_ripple" 
    android:textColor="#303F9F" 
    android:id="@+id/textCanto" 
    android:textSize="25sp" 
    android:layout_height="45dp" /> 

    </LinearLayout> 

這裏是我的java文件:

public class Cantos extends AppCompatActivity { 

ListView lv; 
Context context; 

ArrayList cantoList; 
public static String[] cantos = {"1: Abre Tu Oido", "2: A Cristo Quiero", "3: Acerquese Mi Clamor", "4: A Cristo Yo Alabare", 
     "5: Acude Dios", "6: Adelante", "7: A Dios Canto", "8: Adios Para Siempre", "9: Ahora Senor", "10: A Jesucristo Ven", 
     "11: Alabad A Dios"}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_cantos); 
    initTypeface(); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setTitle(null); 

    context = this; 
    lv = (ListView)findViewById(R.id.ListView); 
    lv.setAdapter(new CustomAdapter(this, cantos)); 

} 

private void initTypeface() { 

    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/AftaSerifThin-Regular.otf"); 
    TextView text = (TextView) findViewById(R.id.toolbarCantos); 
    text.setTypeface(myTypeface); 

} 

}

這裏是我最後的Java文件:

public class CustomAdapter extends BaseAdapter { 

String[] result; 
Context context; 
private static LayoutInflater inflater = null; 


public CustomAdapter(Cantos cantos, String[] cantos1) { 

    result = cantos1; 
    context = cantos; 
    inflater = (LayoutInflater)context. 
      getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

} 

@Override 
public int getCount() { 
    return result.length; 
} 

@Override 
public Object getItem(int position) { 
    return position; 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

public class Holder 
{ 
    TextView tv; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    Holder holder = new Holder(); 
    View rowView; 

    rowView = inflater.inflate(R.layout.cantos_list, null); 
    holder.tv = (TextView) rowView.findViewById(R.id.textCanto); 
    holder.tv.setText(result[position]); 


    return rowView; 
} 
} 

我是新來的android和我gett一竅不通,但我需要這樣的幫助。提前致謝!

+0

shoudnt你正在爲rid.textCanto設置字體,它是列表視圖內的視圖 – Dhina

回答

0

如果你想爲ListView項自定義字體,你應該儘量字體設置爲CustomAdaptor.java類getView(); 看此代碼:

public View getView(int position, View convertView, ViewGroup parent) { 

    Holder holder = new Holder(); 
    View rowView; 

    rowView = inflater.inflate(R.layout.cantos_list, null); 
    holder.tv = (TextView) rowView.findViewById(R.id.textCanto); 
    holder.tv.setText(result[position]); 

    Typeface myTypeface = Typeface.createFromAsset(context.getAssets(), "fonts/AftaSerifThin-Regular.otf"); 
    holder.tv.setTypeface(myTypeface); 

    return rowView; 
} 
+0

謝謝!無論如何,您是否知道如何讓某些列表視圖項目將您發送到某個活動?謝謝! – Alex

+0

setOnItemClickListener到的ListView 'lv.setOnItemClickListener(新AdapterView.OnItemClickListener(){ @覆蓋 公共無效onItemClick(適配器視圖父,觀景,INT位置,長的id){ Toast.makeText(ListActivity.this, 「」+ position,Toast.LENGTH_SHORT).show(); } });' –

+0

先生,謝謝你的迴應,但我不是在尋找這個。我想知道如何編碼,所以當我按下我的列表視圖中的隨機項時,它會將我發送給其他活動。你會知道如何做到這一點? – Alex

0

您可以隨時更改字體在適配器的getView(),但如果你要使用的字體在你的代碼的不同部分多次你總是可以延長TextView的,對於你可以參考一下:

Using custom font in android TextView using xml

這樣一來,每當你需要使用不同的字體,你可以在佈局文件中使用而不是常規的TextView的。

+0

感謝!無論如何,您是否知道如何讓某些列表視圖項目將您發送到某個活動?謝謝! – Alex

+0

是的,當然只要在onItemClick上開始一個新的活動,如果你需要針對不同位置的不同活動,只需檢查位置並相應地啓動該活動即可。檢查http://developer.android.com/training/basics/firstapp/starting-activity.html – holandaGo

+0

如果它幫助你和/或投票,你也應該接受答案。 – holandaGo