【问题标题】:Change font of list fragment on Detail fragment在详细信息片段上更改列表片段的字体
【发布时间】:2013-01-07 05:55:10
【问题描述】:

我正在尝试使用 android 4.0 实现片段。 我在 DummyContent.java 文件的列表片段中添加了三个项目

static {
    // Add 3 sample items.
    addItem(new DummyItem("1", "Videos"));
    addItem(new DummyItem("2", "Images"));
    addItem(new DummyItem("3", "Story"));
}

这些视频、图像、故事出现在片段的左侧,点击每个它的项目都会在右侧显示详细片段的详细信息。

我想更改这些列表视图的字体,但问题是列表片段使用如下系统文本视图

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // TODO: replace with a real list adapter.
    setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(),
            android.R.layout.simple_list_item_activated_1,
            android.R.id.text1, DummyContent.ITEMS));
}

这不允许我将字体应用于android.R.id.text1

请帮帮我

【问题讨论】:

    标签: android android-layout android-fragments android-listfragment android-fonts


    【解决方案1】:

    一种方法是扩展ArrayAdapter 并覆盖getView 方法以获取TextView

    public class MyArrayAdapter<T> extends ArrayAdapter<T> {
    
        public MyArrayAdapter(Context context, List<T> items) {
    
            super(context, android.R.layout.simple_list_item_activated_1, android.R.id.text1, items);
        }
    
        public MyArrayAdapter(Context context, T[] items) {
    
            super(context, android.R.layout.simple_list_item_activated_1, android.R.id.text1, items);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            View view = super.getView(position, convertView, parent);
            TextView textView = (TextView) view.findViewById(android.R.id.text1);
            textView.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "font/segoeuil.tff"));
    
            return view;
        }
    }
    

    然后你可以像这样设置你的适配器:

    setListAdapter(new MyArrayAdapter<DummyContent.DummyItem>(getActivity(), DummyContent.ITEMS);
    

    【讨论】:

    • 感谢您的回答。我尝试了这个解决方案,但唯一的问题是它在textView.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "font/segoeuil.tff")); 线上抛出java.lang.RuntimeException: native typeface cannot be made 如果我评论这一行,那么它可以在没有字体的情况下工作。我无法找出问题
    • 确保您的字体文件确实存在于assets/font/segoeuil.tff 并且您没有任何拼写错误
    • 是的。我检查过,文件存在于位置assets/font/segoeuil.tff。实际上,通过使用相同的字体,我可以更改细节片段的字体。只是它不适用于列表片段
    • 您确定您的字体在font 文件夹中而不是fonts 中吗?
    • 如果不是上述情况,请从您的详细信息片段中复制并粘贴您的 setTypeface 行。它必须是某个地方的简单错字。如果它在 Detail 片段中有效,则使用您在那里使用的完全相同的代码。
    【解决方案2】:

    您可以使用自己的自定义布局而不是预定义的布局 像这样创建一个xml文件

    <?xml version="1.0" encoding="utf-8"?>
    say your layout name be custom.xml
    <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/cust_view"  
        android:layout_width="match_parent" 
        android:textSize="12dp"
        android:padding="6dp"
        android:layout_height="wrap_content" /> 
    

    您可以为此布局指定自己的字体。 然后像这样设置适配器

    setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(),
                R.layout.custom,
                android.R.id.text1, DummyContent.ITEMS));
    

    希望对你有帮助

    【讨论】:

    • 我尝试了这个解决方案,但有一个问题R.layout.custom.xml 不适用于.xml,所以我删除了.xml 并添加为R.layout.custom,所以它不会显示任何错误。但它会使应用程序崩溃
    • 嘿,抱歉,我们不应该包含 .xml,它只是一个打字错误...您得到的究竟是什么异常,请提及,也不需要第三个参数 setListAdapter(new ArrayAdapter (getActivity(), R.layout.custom, DummyContent.ITEMS));
    • 是的,现在它正在根据custom.xml 更改字体大小但是当我尝试将字体设置为TextView tv = ((TextView) findViewById(R.id.cust_view)); Typeface tf = Typeface .createFromAsset(getAssets(), "font/segoeuil.ttf"); tv.setTypeface(tf); setListAdapter(new ArrayAdapter&lt;DummyContent.DummyItem&gt;(getActivity(), R.layout.custom, DummyContent.ITEMS)); 时,它会显示findViewByIdgetAssets 的错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 2013-08-11
    • 2014-07-23
    • 2015-04-05
    • 1970-01-01
    相关资源
    最近更新 更多