Android 活动和片段中出现不同的文本视图

Android 活动和片段中出现不同的文本视图,android,android-fragments,textview,Android,Android Fragments,Textview,我使用了一个特定的布局来设置一个活动的内容视图(),它工作得非常好。但是当我在片段中使用相同的布局时,有一组TextView丢失了它们的文本,我不知道为什么。活动和片段的大小相似(大约为屏幕高度的90%,宽度的100%)。另外-当我调试getText()的TextView时,会返回正确的字母 这两幅图像使用相同的布局来显示内容(这让我相信错误不在那里-请随意纠正我),但片段包含在一些额外的布局中,以提供工具栏。以下是XML代码: <?xml version="1.0" encoding

我使用了一个特定的布局来设置一个
活动的
内容视图()
,它工作得非常好。但是当我在
片段中使用相同的布局时,有一组
TextView
丢失了它们的文本,我不知道为什么。
活动
片段
的大小相似(大约为屏幕高度的90%,宽度的100%)。另外-当我调试
getText()
TextView
时,会返回正确的字母

这两幅图像使用相同的布局来显示内容(这让我相信错误不在那里-请随意纠正我),但片段包含在一些额外的布局中,以提供
工具栏
。以下是XML代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    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="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer"/>

</android.support.v4.widget.DrawerLayout>

所以问题就简单多了。该代码出现在
活动
片段
中:

float keyWidth = keysArray[0].getWidth();
for(int a = 0; a < 27; a++){
    keysArray[a].setTextSize(TypedValue.COMPLEX_UNIT_PX, keyWidth/2);
}
float-keyWidth=keysArray[0].getWidth();
对于(int a=0;a<27;a++){
keysArray[a].setTextSize(TypedValue.COMPLEX\u UNIT\u PX,keyWidth/2);
}
但是它在
活动中的
OnWindowFocusChanged()
中,在
片段中的
OnCreateView()
中。这意味着对于
活动
而言,
keyWidth
是一个合适的大小,而对于
片段
而言,它是0,导致文本大小为0

float keyWidth = keysArray[0].getWidth();
for(int a = 0; a < 27; a++){
    keysArray[a].setTextSize(TypedValue.COMPLEX_UNIT_PX, keyWidth/2);
}