【问题标题】:Add Fragment at Runtime, set its layout at runtime, no layout xml files involved in Android在运行时添加Fragment,在运行时设置其布局,Android中不涉及布局xml文件
【发布时间】:2015-10-14 03:55:56
【问题描述】:

我有一个在 onCreate 中动态创建其布局的活动。它看起来像这样:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /*
     laying out screen at runtime (activity_main.xml is not used).
     */
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT, 
            100);
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);

    // instantiate my class that does drawing on Canvas
    bubble = new Bubble(this);
    bubble .setLayoutParams(lp);
    bubble .setBackgroundColor(MY_COLOR);
    ll.addView(bubble);
    setContentView(ll);
}

所以,根本没有布局,它应该是这样的。

我想添加片段而不是上面的代码,然后作为片段 onCreate() 的一部分,实例化我的 Bubble 类来进行绘图。片段也不应该在布局 XML 文件中定义任何布局,但应该都在运行时。

非常感谢,

【问题讨论】:

  • 到底是什么问题?您只需将您的View 实例化移动到FragmentonCreateView() 方法,并调用return ll; 而不是setContentView(ll);。你甚至不需要LinearLayout,如果Bubble 是唯一的View Fragment 将拥有。
  • 不,你很好 - AppCompatActivity extends FragmentActivity.
  • 您的 Activity 需要在其布局中包含一个 ViewGroup - 通常是 FrameLayout - 它将容纳 Fragment。然后,您将使用FragmentTransaction#add() 方法之一,该方法将int 作为第一个参数,这将是活动中ViewGroup 的ID;例如,R.id.container.
  • 不是没有看到您的代码和堆栈跟踪,但这必须是一个新问题。不过,请先尝试在网站上搜索该错误。我知道我以前见过与该错误有关的问题。
  • @MikeM。非常感谢。我会将您的回复标记为答案,但由于某种原因我不能,它只允许我对您的 cmets 投赞成票/反对票。

标签: android fragment


【解决方案1】:

修改代码以使用Fragments 非常简单。将View 声明移至Fragment,将实例移至其onCreateView() 方法,并返回父ViewGroup。例如:

public class BubbleFragment extends Fragment
{
    Bubble bubble;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        /*
         laying out screen at runtime (activity_main.xml is not used).
         */
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT, 
            100);
        LinearLayout ll = new LinearLayout(getActivity());
        ll.setOrientation(LinearLayout.VERTICAL);

        // instantiate my class that does drawing on Canvas
        bubble = new Bubble(getActivity());
        bubble.setLayoutParams(lp);
        bubble.setBackgroundColor(MY_COLOR);
        ll.addView(bubble);

        return ll;
    }
}

请注意Views 的构造函数参数已更改为getActivity(),因为Fragment 不是Context。另外,如果没有更多的Views 将被添加到Fragment,您可以省略LinearLayout,只返回Bubble 对象。

Activity 需要自己的布局,其中包括 ViewGroup - 通常是 FrameLayout - 以容纳 Fragment。无论您是使用预定义的 XML 布局还是动态生成它都无关紧要。您只需要一个 ViewGroup 的 ID,您可以将其传递给 FragmentTransaction#add() 方法之一。例如:

BubbleFragment keplerFragment = new BubbleFragment(...);
getFragmentManager().beginTransaction().add(R.id.content, keplerFragment, TAG_KEPLER_FRAGMENT);

而且,正如 cmets 中所述,由于 AppCompatActivity extends FragmentActivity,您无需更改 Activity 的超类。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-08
  • 1970-01-01
  • 2020-03-04
相关资源
最近更新 更多