片段使用滑动视图appcompat的资产

时间:2014-03-28 23:56:08

标签: android android-fragments

我正在使用片段而且我无法使用这些资产来更改我的信件。我正在使用appcompat的滑动页面。这是我的代码:

public class AcercaFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.acerca, container, false);
        TextView logouno = (TextView) getView().findViewById(R.id.logouno);
        Typeface typeface = Typeface.createFromAsset(getActivity()
                .getAssets(), "fonts/Roboto-Thin.ttf");
        logouno.setTypeface(typeface);

        return view;
    }
}

2 个答案:

答案 0 :(得分:0)

尝试这个小小的改变:

public class AcercaFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.acerca, container, false);
        TextView logouno = (TextView) view.findViewById(R.id.logouno);
        Typeface typeface = Typeface.createFromAsset(getActivity()
                .getAssets(), "fonts/Roboto-Thin.ttf");
        logouno.setTypeface(typeface);

        return view;
    }
}

您应用字体的方式还可以,但您必须从视图view获取TextView。

答案 1 :(得分:0)

您无法在getView()方法中访问onCreateView的观看次数。在这里,您应该只返回一个膨胀的视图:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
{
    return inflater.inflate(R.layout.acerca, container, false);
}

然后,初始化UI元素,如下所示:

@Override
public void onActivityCreated(Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);
    TextView logouno =(TextView) getView().findViewById(R.id.logouno);
    Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto-Thin.ttf");
    logouno.setTypeface(typeface);
}