androidでプリロードレイアウトを使用する方法
Androidでは、レイアウトをプリロードするには以下のメソッドを使うことができます。
- レイアウトインフレータ
- 膨張する()
LayoutInflater inflater = LayoutInflater.from(context);
View layout = inflater.inflate(R.layout.layout_name, null);
// 进行后续操作
- ビュースタブ
レイアウトファイル内のプレースホルダー:
<ViewStub
android:id="@+id/stub_layout"
android:layout="@layout/layout_name"
android:inflatedId="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
ViewStubを利用してレイアウトをコードからロードする:
ViewStub stubLayout = findViewById(R.id.stub_layout);
stubLayout.inflate();
View layout = findViewById(R.id.layout);
// 进行后续操作
- ネイティブな日本語で文を言い換えてください、オプションは1つだけです:フラグメント
- onCreateView()
public class MyFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.layout_name, container, false);
}
}
アクティビティでフラグメントを使用するレイアウト
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, new MyFragment());
fragmentTransaction.commit();
目的のユースケースに応じて、いくつかの一般的なプリロードレイアウトのテクニックから適切な方法を選ぶことができます。