Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ListView中的自定义字体_Java_Android_Listview - Fatal编程技术网

Java ListView中的自定义字体

Java ListView中的自定义字体,java,android,listview,Java,Android,Listview,因此,我试图将资产文件夹中已有的自定义字体(hello.ttf)设置为ListView。下面是我的java和xml文件 faListFragment.java public class faListFragment extends Fragment { public faListFragment() { // Required empty public constructor } @Override public View onCreate

因此,我试图将资产文件夹中已有的自定义字体(hello.ttf)设置为ListView。下面是我的java和xml文件

faListFragment.java

public class faListFragment extends Fragment {


    public faListFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_fa_list, container, false);

        String[] faList = { "One",
                            "Two",
                            };

        ListView listView = (ListView) view.findViewById(R.id.listFa);


        ArrayAdapter<String> listviewAdapter = new ArrayAdapter<String>(
              getActivity(),
              android.R.layout.simple_list_item_1,
              faList
       );




        listView.setAdapter(listviewAdapter);

        // Inflate the layout for this fragment
        return view;
    }

}
公共类faListFragment扩展了片段{
公法团(){
//必需的空公共构造函数
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u fa\u list,container,false);
字符串[]faList={“一”,
“两个”,
};
ListView=(ListView)view.findViewById(R.id.listFa);
ArrayAdapter listviewAdapter=新的ArrayAdapter(
getActivity(),
android.R.layout.simple\u list\u item\u 1,
法利斯特
);
setAdapter(listviewAdapter);
//为该碎片膨胀布局
返回视图;
}
}
这是我的fragment\u fa\u list.xml

<FrameLayout 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"
    tools:context="com.me.hello.faListFragment">

    <!-- TODO: Update blank fragment layout -->

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listFa" />
</FrameLayout>


我尝试过几种添加字体的方法,但似乎不太明白如何添加

您必须在布局文件夹中创建一个布局xml。在该布局xml中,您必须使用自定义字体使用自定义文本视图

对于CustomTextView链接:

行项目.xml

<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:layout_height="wrap_content"
    android:layout_margin="15dp"
    android:background="#00000000"
    android:orientation="vertical">




    <pakagename.MyTextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"

        android:text="CEO"
        android:textColor="#000"
        android:textSize="16dp" />


</RelativeLayout>


我没有完全理解这一点。对于TextView的CustomFont,您必须这样做。如果你发现一些新的东西,请也建议我。如果你能再详细一点,我会很感激的。我对java和android开发非常陌生。在(R.layout.row_item,R.id.text)的末尾应该有一个(,),否则代码会产生语法错误。否则,这将非常有效。请更正以便我能接受答案
public class MyTextView extends TextView {

    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

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

    public MyTextView(Context context) {
        super(context);
        init();
    }

    private void init() {
        if (!isInEditMode()) {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf");
            setTypeface(tf);
        }
    }

}
<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:layout_height="wrap_content"
    android:layout_margin="15dp"
    android:background="#00000000"
    android:orientation="vertical">




    <pakagename.MyTextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"

        android:text="CEO"
        android:textColor="#000"
        android:textSize="16dp" />


</RelativeLayout>