【问题标题】:Create fragment and set parameter programmatically以编程方式创建片段并设置参数
【发布时间】:2016-01-25 00:42:33
【问题描述】:

我在创建片段的代码中遇到以下问题,这没关系。
我的问题是当我尝试设置他和参数时。
这是我的代码。

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        0, ViewGroup.LayoutParams.WRAP_CONTENT);
params.weight = 1.0f;

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentImageList myFragment = new FragmentImageList();

fragmentTransaction.add(631, myFragment, "fragmentTag");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

myFragment.getView().setLayoutParams(params);

然后在控制台中出现以下错误。

Attempt to invoke virtual method 'void android.view.View.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference

这是有道理的,因为尚未创建 Fragment 的视图。 但是如何设置已创建片段的“参数”。

【问题讨论】:

    标签: android layout params layoutparams fragmentmanager


    【解决方案1】:

    你要传递给的631:

      fragmentTransaction.add(631, myFragment, "fragmentTag");  
    

    应该是您要在其上添加片段的视图 ID。 确保 631 是该视图的 id,否则像 R.id.yourViewId 一样传递

    【讨论】:

    • 因为在此之前我在代码中创建了 LinearLayout,这是他的 ID。目的是在没有 GridLayout 的情况下创建一个由行和列组成的网格。
    【解决方案2】:

    感谢您的回答,我觉得自己很愚蠢。
    我的目标是用 Fragment 创建一个网格。
    但是这个带参数的整个方案是完全没有必要的。

    @Hitesh Sahu,再次感谢您。
    Т您的答案的一部分很关键。

    片段就像占位符,它们会自动填充其持有量 容器视图(例如 FrameLyout )并且您不需要分配它们的 宽度和高度手动。


    只需创建另一个 res/layout 文件以在片段中使用它。
    仅适用于这个网格:)

    【讨论】:

      【解决方案3】:

      我不知道您的要求是什么,但我认为您应该膨胀视图而不是片段。

      片段就像占位符,它们会自动填充其持有的容器视图(例如 FrameLyout ),您无需手动分配它们的宽度和高度。但是如果你仍然想这样做,你应该在你的 Fragment 类的 onCreate View 中这样做

          @Override
              public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                  rootView = inflater.inflate(R.layout.home_fragment, container, false);
      
       // assign new layout params on rootView here
      

      ------------------------------------------或者---- -------------------------------------------------- ------------

      使用布局充气器来做同样的事情

             //Use Layout inflater
              LayoutInflater inflater = (LayoutInflater) context
                              .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      
              //Load layout xml of child layout you want to add
              View childView= inflater.inflate(R.layout.viewpager_place_holder, null);
      
      
              //Get and Assign values on views of child view
             ((TextView) childView.findViewById(R.id.pager_title))
                                  .setText("Test");
      
             // Set width and margin on child layout
             LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                              LinearLayout.LayoutParams.MATCH_PARENT,
                              LinearLayout.LayoutParams.WRAP_CONTENT);
      
              layoutParams.setMargins(
                                  UtilFunctions.getUtilFunctions().dpToPixels(5,
                                          context.getResources()),
                                  UtilFunctions.getUtilFunctions().dpToPixels(5,
                                          context.getResources()),
                                  UtilFunctions.getUtilFunctions().dpToPixels(5,
                                          context.getResources()),
                                  UtilFunctions.getUtilFunctions().dpToPixels(5,
                                          context.getResources()));
      
            //Add child to the root layout
            rootLayout.addView(childView, layoutParams);
      

      【讨论】:

      • 感谢您的回复,不要完全使用它,但它给了我一个想法:) 感谢您的帮助。
      猜你喜欢
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-19
      • 2021-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多