android - 如何在片段的 onCreateView() 方法中使用布局类?
问题描述
我有 res/layout/main_menu_layout.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<sidekick.cpp.CMainMenuButton
android:id="@+id/manualMainMenuButton"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textSize="18sp"
android:text="@string/main_menu_button_manual_text" />
<sidekick.cpp.CMainMenuButton
android:id="@+id/editorMainMenuButton"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textSize="18sp"
android:text="@string/main_menu_button_editor_text" />
<sidekick.cpp.CMainMenuButton
android:id="@+id/exitMainMenuButton"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textSize="18sp"
android:text="@string/main_menu_button_exit_text" />
</LinearLayout>
此布局用于以下类:
package sidekick.cpp; import android.widget.*; import android.content.*; import android.view.*; public class CMainMenuLayout extends LinearLayout { public CMainMenuLayout(Context context) { super(context); this.setOrientation(VERTICAL); View view = LayoutInflater.from(getContext()).inflate( R.layout.main_menu_layout, null); this.addView(view); } }
现在我想在片段中使用布局。如果我在其中使用布局的 ID ( R.layout.main_menu_layout
),onCreateView()
那么下面的代码似乎可以工作:
public class CMainMenuFragment extends Fragment implements View.OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_menu_layout, container, false);
}
...
}
但我需要使用 CMainLayout 类,而不是R.layout.main_menu_layout
. 下面的代码似乎也可以工作,但它正确吗?
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
CMainMenuLayout layout = new CMainMenuLayout(this.getContext());
return layout;
}
解决方案
一、可以CMainMenuLayout
直接 inflate xml layout in
public CMainMenuLayout(Context context) {
super(context);
this.setOrientation(VERTICAL);
LayoutInflater.from(getContext()).inflate(R.layout.main_menu_layout, this, true);
}
然后CMainMenuLayout
在 xml 中使用类CMainMenuFragment
。例如
res/layout/main_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<sidekick.cpp.CMainMenuLayout
android:id="@+id/mainMenuLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
并且在CMainMenuFragment
public class CMainMenuFragment extends Fragment implements View.OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_menu_fragment, container, false);
}
...
}
推荐阅读
- php - 无法获取 url php 中的第二个参数
- azure-stream-analytics - 我们可以为对多种输出类型有多个查询的 Azure 流分析作业进行查询并行化/分区吗?
- javascript - 如何使用按钮属性克隆 div?
- github - GitHub Actions 单个存储库中的多个操作
- ios - 如何将 UIPageViewController 添加到 UIStackView(在 UIScrollView 内)
- java - 是否可以使用 Spring Boot Admin 工具监控 Spring Batch 作业执行状态?
- c - 带有没有返回值的参数的函数在 turbo c 上无法正常工作
- python - python docx上的布局问题
- c# - 当我在 Visual Studio 2019 中以调试方式启动应用程序时经常出现问题
- rest - 使用 REST API 或 CURL 请求从特定 Jenkins 项目中删除/删除用户