首页 > 解决方案 > 如何在片段的 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;
}

标签: androidandroid-layoutandroid-fragments

解决方案


一、可以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);

}
    ...

}

推荐阅读