2016-04-28 59 views
1

我想在下面的代码中将字体设置为listview。所有相关的文件都附上。如何在通过ArrayAdapter放置ArrayList的ListView上设置字体

SearchClient.java

Toolbar mToolbar; 
EditText search; 
ListView listView; 
ArrayAdapter<String> adapter; 
ArrayList<HashMap<String, String>> productList; 
Typeface typeface; 
DatabaseHelper databaseHelper; 
//Clients List 
List<String> client_name = new ArrayList<>(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_search_client); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    mToolbar = (Toolbar) findViewById(R.id.toolbar_search_client); 
    setSupportActionBar(mToolbar); 

    typeface = Typeface.createFromAsset(getAssets(),"fonts/ProductSans-Regular.ttf"); 
    init(); 

    search.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      search.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#ffffff"))); 
     } 
    }); 

    databaseHelper = new DatabaseHelper(this); 

    Cursor res = databaseHelper.TABLE_CLIENTS_view(); 
    while (res.moveToNext()) { 
     client_name.add(res.getString(1)); 
    } 

    adapter = new ArrayAdapter<String>(this, R.layout.search_list_item, R.id.search_client_name, client_name); 
    listView.setAdapter(adapter); 

    search.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { 
      // When user changed the Text 
      SearchClient.this.adapter.getFilter().filter(cs); 
     } 

     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
             int arg3) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable arg0) { 
      // TODO Auto-generated method stub 
     } 
    }); 

} 

public void init(){ 
    search = (EditText) findViewById(R.id.search_client); 
    search.setTypeface(typeface); 
    listView = (ListView) findViewById(R.id.listView_search_client); 
} 

activity_search_client.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:focusableInTouchMode="true" 
    android:focusable="true" 
    android:id="@+id/rel_lay_client_search" 
    tools:context="com.example.monilandharia.invoice.SearchClient"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:id="@+id/container_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <include 
       android:id="@+id/toolbar_search_client" 
       layout="@layout/toolbar_search" /> 
     </LinearLayout> 

     <ListView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="10dp" 
      android:id="@+id/listView_search_client" /> 


    </LinearLayout> 

</RelativeLayout> 

search_list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView android:id="@+id/search_client_name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="10dip" 
     android:textSize="16dip" 
     android:textStyle="bold"/> 

</LinearLayout> 

并感谢很多提前的帮助!

+0

我想你需要一个自定义的ArrayAdapter。 – yennsarah

+0

请参阅http://androidtrainningcenter.blogspot.in/2013/07/applying-custom-font-in-entire-android.html。 –

+0

你不能设置列表视图类型的脸。在你的适配器上设置类型面。 –

回答

1

您可以像下面那样创建自定义文本视图,并在自定义文本视图类中设置所需的字体,如下所示。

public class CustomTextView extends TextView { 

     public CustomTextView(Context context, AttributeSet attrs) { 
      super(context, attrs); 
     } 

     private void init(Context context, AttributeSet attrs) { 
      Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/ProductSans-Regular.ttf"); 
      this.setTypeface(typeface); 
     } 
    } 

,并在你的XML(search_list_item.xml)文件好心做了以下变化。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <com.example.CustomTextView android:id="@+id/search_client_name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="10dip" 
     android:textSize="16dip" 
     android:textStyle="bold"/> 

</LinearLayout> 

现在,类型面将应用于textview。

+0

检查下一个代码 –

0

1 - 创建一个目录assetsmain目录内

2-下载来自互联网所需的字体和文件复制到资产

3-在适配器

Typeface name_For_Typeface; 

name_For_Typeface= Typeface.createFromAsset(getContext().getAssets(), "yourfont.ttf"); 

的TextView .setTypeface(name_For_Typeface);

+0

是的,我知道它可以通过CustomAdapter,但尝试@Jeeva Nandhan建议的那个!这很酷...... –

+0

这是实现目标的一种方式,真棒找到你的解决方案 –

0

哦谢谢,但你犯了一个错误,但我改变了。检查了这一点:

您的代码:

/* Constructor */ public CustomTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    private void init(Context context, AttributeSet attrs) { 
     Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/ProductSans-Regular.ttf"); 
     this.setTypeface(typeface); 
    } 

你忘了调用init(),所以也没做任何改变。你可以在初始化中传递上下文和attrs,如init(context,attrs)。你也可以按照下面的方式完成,而不是我:

/* Constructor */ public CustomTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/ProductSans-Regular.ttf"); 
    this.setTypeface(typeface); 
} 
+0

请注意,答案不是作为与其他答疑者交谈的设备。如果你通过评论系统添加反馈会更好,一旦你有50个重要点你就可以做到。在一定程度上,这个答案确实包含了你自己的答案,但它有可能无法在“不是答案”评论中生存下来。值得记住你的下一个答案? – halfer

相关问题