Android 附加后,膨胀视图的id将由片段的id重新计算?

Android 附加后,膨胀视图的id将由片段的id重新计算?,android,fragment,getview,Android,Fragment,Getview,我在活动的布局文件中创建了一个片段。例如: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <fragment android:layo

我在活动的布局文件中创建了一个片段。例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <fragment android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:id="@+id/article_fragment"
        android:name="com.user.domain.ArticleFragment"/>
</LinearLayout>
ariticle_视图布局xml如下所示:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/article"
    android:textSize="18sp"
    android:padding="16dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</TextView>
这就是问题的原因吗?

您必须找到片段的子视图,如


意思是你想为文章_片段找到一个id???我想在视图创建之后获得视图实例你可以为视图创建全局变量,并在onCreateView方法中将该实例引用到返回的视图中。是的,我们可以在onCreateView方法中这样做。我只是好奇为什么视图实例的id在创建后会变为片段的id?是的,我们可以在OnCreateView函数中按视图的id存储视图实例,但我们也可以在视图创建后按片段的id获取视图的实例。在这种情况下,v是TextView,因为它是父对象。哦,我没有注意到这一点。您是否尝试将膨胀视图强制转换为文本视图?
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/article"
    android:textSize="18sp"
    android:padding="16dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</TextView>
The system inserts the View returned by the fragment directly in place of the <fragment> element.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.article_view, container, false);
TextView yourText = v.findViewById(R.id.article);
return v;
}