bat*_*ope 5 android android-fragments android-4.4-kitkat child-fragment android-5.1.1-lollipop
我开发了一个应用程序,它使用片段作为片段的一部分。
public class LoginFragment extends Fragment
EditTextWithCameraButtonFrag userNameFrag;
EditTextWithCameraButtonFrag passwordFrag;
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
userNameFrag = (EditTextWithCameraButtonFrag)getChildFragmentManager()
.findFragmentById(R.id.fragment_username);
passwordFrag = (EditTextWithCameraButtonFrag) getChildFragmentManager()
.findFragmentById(R.id.fragment_password);
Run Code Online (Sandbox Code Playgroud)
EditTextWithCameraButtonFrag是 Fragment 的子类型。这段代码就像一个魅力,在 android 5.1.1 上运行。但是,在 Android 4.4.2 上运行它会返回 userNameFrage 和 passwordFrag 为 null。所以看起来,返回的 Child FragmentManager 找不到这些字段。
与4.4.3一起使用时有什么需要注意的吗getChildFragmentManager()?
提前致谢!
编辑:这是主要片段的 XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/huge"
android:orientation="vertical" >
<TextView
android:id="@+id/login_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:gravity="center"
android:textColor="@color/white"
android:textSize="40dp" />
<fragment
android:id="@+id/fragment_username"
android:name="com.example.app.fragments.EditTextWithCameraButtonFrag"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/fragment_password"
android:name="com.example.app.fragments.EditTextWithCameraButtonFrag"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/huge"
android:layout_marginStart="@dimen/huge"
android:layout_marginTop="@dimen/normal"
android:padding="@dimen/half"
android:text="Login" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这是子片段的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/editText_with_button_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/text_input_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0.75"
android:background="@drawable/edit_text_login_top"
android:inputType="text"
android:padding="@dimen/normal" />
<Button
android:id="@+id/scan_text_view"
android:layout_width="wrap_content"
android:background="@android:color/transparent"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/text_input_field"
android:layout_alignTop="@+id/text_input_field"
android:layout_alignBottom="@+id/text_input_field"
/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我刚刚解决了它-有点。经过大量阅读后,似乎可以通过 XML 声明嵌套片段,但这不是最佳实践。
FrameLayout所以我决定在 XML 中包含s,并通过 . 添加嵌套的 Fragment FragmentTransaction。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/huge"
android:orientation="vertical" >
<TextView
android:id="@+id/login_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:gravity="center"
android:textColor="@color/white"
android:textSize="40dp" />
<FrameLayout
android:id="@+id/fragment_username"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/fragment_password"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/huge"
android:layout_marginStart="@dimen/huge"
android:layout_marginTop="@dimen/normal"
android:padding="@dimen/half"
android:text="Login" />
</LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
在主片段的 onCreate 中:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// create userNameFrag
EditTextWithCameraButtonFrag userfragment = new EditTextWithCameraButtonFrag();
getChildFragmentManager().beginTransaction().replace(R.id.fragment_username, userfragment).commit();
// create passwordFrag
EditTextWithCameraButtonFrag passfragment = new EditTextWithCameraButtonFrag();
getChildFragmentManager().beginTransaction().replace(R.id.fragment_password, passfragment).commit();
}
Run Code Online (Sandbox Code Playgroud)
这似乎工作得很好,而且 - 另一个优点 - 我还摆脱了重复的 id 错误。
| 归档时间: |
|
| 查看次数: |
3919 次 |
| 最近记录: |