【问题标题】:How to set Typeface on ListView which has an ArrayList placed through ArrayAdapter如何在通过 ArrayAdapter 放置 ArrayList 的 ListView 上设置字体
【发布时间】:2016-08-23 03:45:11
【问题描述】:

我想在下面的代码中将字体设置为列表视图。附上所有相关文件。

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>

提前非常感谢您的帮助!!

【问题讨论】:

标签: android listview arraylist android-arrayadapter typeface


【解决方案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>

现在字体将应用于文本视图。

【讨论】:

    【解决方案2】:

    1- 在main 目录中创建一个目录assets

    2- 从互联网上下载所需的字体并将文件复制到资产中

    3- 在你的适配器中

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

    textview.setTypeface(name_For_Typeface);

    【讨论】:

    • 是的,我知道通过 CustomAdapter 是可能的,但请尝试@Jeeva Nandhan 建议的那个!太酷了……
    • 这是实现目标的一种方式,你找到了解决方案真棒
    【解决方案3】:

    哦,谢谢,但你犯了一个错误,但我改了。看看这个:

    您的代码:

        /* 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() 所以它没有做任何改变。你可以像init(context,attrs)一样在init中传递上下文和属性。你也可以像下面那样做,而不是我做了:

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

    【讨论】:

    • 请注意,答案并非旨在与其他回答者交谈的设备。如果您通过 cmets 系统添加反馈会更好,一旦您获得 50 个代表点,您就可以这样做。在一段时间内,这个答案确实包含您自己的答案,但它可能无法在“非答案”审查中幸存下来。下一个答案值得记住吗?
    猜你喜欢
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多