Android 在片段中设置新布局

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12222925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:04:54  来源:igfitidea点击:

Set new layout in fragment

androidandroid-fragments

提问by Dacto

I'm trying to change the layout of a fragment during runtime under a particular condition.

我正在尝试在特定条件下在运行时更改片段的布局。

The initial layout in inflated within the onCreateView():

在 onCreateView() 中膨胀的初始布局:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.cancel_video, null);
    }

Then sometime later within the fragment code I would like to replace the initial layout with some other layout.

然后稍后在片段代码中,我想用其他一些布局替换初始布局。

I've tried a few things so far; this is the latest that I have:

到目前为止,我已经尝试了一些东西;这是我最新的:

private void Something(){
    if(checkLicenseStatus(licenseStatus, statusMessage)){
                View vv = View.inflate(getActivity(), R.layout.play_video, null);
                //more code
    }
}

How can I accomplish this?

我怎样才能做到这一点?

采纳答案by Marcin Orlowski

You cannot replace the fragment's layout once it is inflated. If you need conditional layouts, then you either have to redesign your layout and break it down into even smaller elemens like Fragments. Alternatively you can group all the layout elements into sub containers (like LinearLayout), then wrap them all in RelativeLayout, position them so they overlay each other and then toggle the visibility of these LinearLayouts with setVisibility()when and as needed.

一旦膨胀,您就无法替换片段的布局。如果您需要条件布局,那么您要么必须重新设计您的布局并将其分解为更小的元素,例如Fragments. 或者,您可以将所有布局元素分组到子容器中(如LinearLayout),然后将它们全部包裹在 中RelativeLayout,放置它们以使它们彼此重叠,然后在需要时根据需要切换这些LinearLayouts的可见性setVisibility()

回答by Tonithy

Use a FragmentTransaction through the FragmentManger

通过 FragmentManger 使用 FragmentTransaction

FragmentManager fm = getFragmentManager();

if (fm != null) {
    // Perform the FragmentTransaction to load in the list tab content.
    // Using FragmentTransaction#replace will destroy any Fragments
    // currently inside R.id.fragment_content and add the new Fragment
    // in its place.
    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.fragment_content, new YourFragment());
    ft.commit();
}

The code for the class YourFragment is just a LayoutInflater so that it returns a view

YourFragment 类的代码只是一个 LayoutInflater,因此它返回一个视图

public class YourFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.your_fragment, container, false);

        return view;
    }   

}

回答by Ujjal Suttra Dhar

Yes, I have done this in following way. When I need to set a new layout(xml), the following code snippet should be executed.

是的,我已经通过以下方式做到了这一点。当我需要设置新的布局(xml)时,应该执行以下代码片段。

  private View mainView;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup containerObject, Bundle savedInstanceState){
    super.onCreateView(inflater, containerObject, savedInstanceState);

        mainView = inflater.inflate(R.layout.mylayout, null);
        return mainView;
  }

  private void setViewLayout(int id){
    LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mainView = inflater.inflate(id, null);
    ViewGroup rootView = (ViewGroup) getView();
    rootView.removeAllViews();
    rootView.addView(mainView);
  }

Whenever I need to change the layout I just call the following method

每当我需要更改布局时,我只需调用以下方法

    setViewLayout(R.id.new_layout); 

回答by wenmin92

I will change whole layout. You can change only a specific portion of your layout.

我会改变整个布局。您只能更改布局的特定部分。

  1. Add a root FrameLayoutin your_layout.xml, it contains nothing.

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fl_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
  2. Set your content in the java code.

    ViewGroup flContent = findViewById(R.id.fl_content);
    
    private void setLayout(int layoutId) {
        flContent.removeAllViews();
        View view = getLayoutInflater().inflate(layoutId, flContent, false);
        flContent.addView(view);
    }
    
  3. You can change layoutIdfree at some point.
    If you have some listeners, you must set again.

  1. 添加根FrameLayoutyour_layout.xml,它包含了什么。

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fl_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
  2. 在 java 代码中设置您的内容。

    ViewGroup flContent = findViewById(R.id.fl_content);
    
    private void setLayout(int layoutId) {
        flContent.removeAllViews();
        View view = getLayoutInflater().inflate(layoutId, flContent, false);
        flContent.addView(view);
    }
    
  3. 您可以layoutId在某些时候免费更改。
    如果您有一些听众,则必须重新设置。