片段方向更改后不起作用的方法



在我的Android项目中,我使用类方法为所有视图设置字体类型,这是方法:

    public  void overrideFonts(final Context context, final View v ) {
    try {
        if (v instanceof ViewGroup) {
            ViewGroup vg = (ViewGroup) v;
            for (int i = 0; i < vg.getChildCount(); i++) {
                View child = vg.getChildAt(i);
                overrideFonts(context, child);
            }
        } else if (v instanceof TextView) {
            ((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(),"font/aFont.ttf"));
        }
    } catch (Exception e) {
    }
}

它在我的片段上工作正常,除非我更改设备的方向。 之后,即使将方向恢复为纵向,此方法也不起作用!我尝试了保存实例状态(我认为这与我的问题无关(....那么问题出在哪里?!

这是我在其中使用此方法的片段类。

public class aboutUs extends Fragment {
MUF option;
RelativeLayout relativeLayout;

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    option = new MUF(getActivity(), getContext());
    relativeLayout = (RelativeLayout) getActivity().findViewById(R.id.aboutus_fragment);
    option.overrideFonts(getContext(), relativeLayout);
    Toast.makeText(getActivity(), "onActivityCreated", Toast.LENGTH_SHORT).show();

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_about_us, container, false);
    return view;

}

}
<activity
        android:name=".ui.activities.MainActivity"//Your fragment's activity
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:screenOrientation="portrait" />

从您的Android清单中.xml只需从android:configChanges中删除"orientation""screenSize",如果添加到片段的活动中,也可以android:screenOrientation="portrait"

最新更新