2016-03-01 4 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> 

Это для моей ListView.

Вот мой заказ ListView 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> 

Вот мои ява файлы:

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, и я получаю но мне нужна помощь в таких вещах. Заранее спасибо!

+0

shoudnt вы устанавливаете шрифт для R.id.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() { @Override общественного недействительный onItemClick (AdapterView родителя, Вид вид, внутр позиция, длинный идентификатор) { Toast.makeText (ListActivity.this, «+ позиция, 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

Смежные вопросы