如何将片段添加到程序生成的布局? [英] How to add a fragment to a programmatically generated layout?

查看:113
本文介绍了如何将片段添加到程序生成的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有它产生的碎片以下code工作,但只有当我加入他们存在于我的XML文件中的线性布局。

 的LinearLayout fragmentsLayout =(的LinearLayout)findViewById(R.id.foodItemActvity_linearLayout_fragments);
FragmentManager fragMan = getFragmentManager();
FragmentTransaction fragTransaction = fragMan.beginTransaction();

片段myFrag =新ImageFragment();
fragTransaction.add(R.id.foodItemActvity_linearLayout_fragments,myFrag,片段+ fragCount);
fragTransaction.commit();
 

现在如果我想的片段添加到确实NTO线性布局已经在格兰XML文件中存在,如

 的LinearLayout的RowLayout =新的LinearLayout();
 

第2部分:

 片段frag1 = generateAppropriateFragment(TYPE1);
    片段frag2 = generateAppropriateFragment(2型);

    的LinearLayout fragmentsLayout =(的LinearLayout)findViewById(R.id.foodItemActvity_linearLayout_fragments);
    LinearLayout中RowLayout的=新的LinearLayout(本);
    rowLayout.setId(12345); //添加计数器结束

    fragmentsLayout.addView(RowLayout的);
    。getFragmentManager()的BeginTransaction()加(rowLayout.getId(),frag1,fragment_grandchild+ fragCount).commit()。
    fragCount ++;
    。getFragmentManager()的BeginTransaction()加(rowLayout.getId(),frag2,fragment_grandchild+ fragCount).commit()。
    fragCount ++;
 

解决方案

在某些时候,我想你会增加你的编程方式创建的LinearLayout以你为.xml定义了一些根布局。 这只是我的一个建议,许多解决方案的可能,但它的工作原理: 简单的设置一个ID为编程创建布局,并把它添加到您在定义的.xml根布局,然后点击使用集ID添加的片段。

这可能是这样的:

 的LinearLayout的RowLayout =新的LinearLayout();

FragmentManager fragMan = getFragmentManager();
FragmentTransaction fragTransaction = fragMan.beginTransaction();

rowLayout.setId(whateveryouwantasid);

//添加RowLayout的根布局的地方在这里

片段myFrag =新ImageFragment();
fragTransaction.add(rowLayout.getId(),myFrag,片段+ fragCount);
fragTransaction.commit();
 

简单地选择的任何整数值你想作为ID:

  rowLayout.setId(12345);
 

如果您使用的是code不只是一次上面的线,它很可能是明智的想出一个办法,创造出独特-ID的,以避免重复

更新:

这里是它应该怎么做充分code:的 (此code为测试和工程) 我加入两种片段与水平方向一个的LinearLayout,导致片段对准彼此相邻。另外请注意,是我用一个固定的高度和200dp宽度,使一个片段不使用全屏,因为它会与match_parent。

MainActivity.java:

 公共类MainActivity延伸活动{

    @燮pressLint(NewApi)
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        的LinearLayout fragContainer =(的LinearLayout)findViewById(R.id.llFragmentContainer);

        的LinearLayout LL =新的LinearLayout(本);
        ll.setOrientation(LinearLayout.HORIZONTAL);

        ll.setId(12345);

        。getFragmentManager()的BeginTransaction()加(ll.getId(),TestFragment.newInstance(我的frag 1),someTag1)提交()。
        。getFragmentManager()的BeginTransaction()加(ll.getId(),TestFragment.newInstance(我是FRAG 2),someTag2)提交()。

        fragContainer.addView(Ⅱ);
    }
}
 

TestFragment.java:

 公共类TestFragment扩展片段{

    公共静态TestFragment的newInstance(字符串文本){

        TestFragment F =新TestFragment();

        叠B =新包();
        b.putString(文字,文本);
        f.setArguments(B);
        返回F;
    }

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){

        视图V = inflater.inflate(R.layout.fragment,集装箱,假);

        ((TextView中)v.findViewById(R.id.tvFragText))的setText(getArguments()的getString(文本));
        返回伏;
    }
}
 

activity_main.xml:

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID / rlMain
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:填充=5DP
    工具:上下文=MainActivity。>

    <的TextView
        机器人:ID =@ + ID / textView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=@字符串/参考hello world/>

    <的LinearLayout
        机器人:ID =@ + ID / llFragmentContainer
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_alignLeft =@ + ID / textView1
        机器人:layout_below =@ + ID / textView1
        机器人:layout_marginTop =19dp
        机器人:方向=垂直>
    < / LinearLayout中>
< / RelativeLayout的>
 

fragment.xml之:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =200dp
    机器人:layout_height =200dp>

    <的TextView
        机器人:ID =@ + ID / tvFragText
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_centerHorizo​​ntal =真
        机器人:layout_centerVertical =真
        机器人:文本=/>

< / RelativeLayout的>
 

和这是上面code中的结果:(这两个片段被下彼此对准)

您都相当受欢迎,我想:)

I have the following code working which generates fragments, but only if I am adding them to a linear layout which exists in my xml file.

LinearLayout fragmentsLayout = (LinearLayout) findViewById(R.id.foodItemActvity_linearLayout_fragments);
FragmentManager fragMan = getFragmentManager();
FragmentTransaction fragTransaction = fragMan.beginTransaction();

Fragment myFrag= new ImageFragment();
fragTransaction.add(R.id.foodItemActvity_linearLayout_fragments, myFrag , "fragment" + fragCount);
fragTransaction.commit();

Now what if I want to add that fragment to a linear layout that does nto already exist in teh xml file such as

LinearLayout rowLayout = new LinearLayout();

Part 2:

    Fragment frag1 = generateAppropriateFragment(type1);
    Fragment frag2 = generateAppropriateFragment(type2);

    LinearLayout fragmentsLayout = (LinearLayout) findViewById(R.id.foodItemActvity_linearLayout_fragments);
    LinearLayout rowLayout = new LinearLayout(this);
    rowLayout.setId(12345); // add counter to end

    fragmentsLayout.addView(rowLayout);     
    getFragmentManager().beginTransaction().add(rowLayout.getId(), frag1, "fragment_grandchild" + fragCount).commit();
    fragCount++;
    getFragmentManager().beginTransaction().add(rowLayout.getId(), frag2, "fragment_grandchild" + fragCount).commit();
    fragCount++;

解决方案

At some point, I suppose you will add your programatically created LinearLayout to some root layout that you defined in .xml. This is just a suggestion of mine and probably one of many solutions, but it works: Simply set an ID for the programatically created layout, and add it to the root layout that you defined in .xml, and then use the set ID to add the Fragment.

It could look like this:

LinearLayout rowLayout = new LinearLayout();

FragmentManager fragMan = getFragmentManager();
FragmentTransaction fragTransaction = fragMan.beginTransaction();

rowLayout.setId(whateveryouwantasid);

// add rowLayout to the root layout somewhere here

Fragment myFrag = new ImageFragment();
fragTransaction.add(rowLayout.getId(), myFrag , "fragment" + fragCount);
fragTransaction.commit();

Simply choose whatever Integer value you want for the ID:

rowLayout.setId(12345);

If you are using the above line of code not just once, it would probably be smart to figure out a way to create unique-IDs, in order to avoid duplicates.

UPDATE:

Here is the full code of how it should be done: (this code is tested and works) I am adding two Fragments to a LinearLayout with horizontal orientation, resulting in the Fragments being aligned next to each other. Please also be aware, that I used a fixed height and width of 200dp, so that one Fragment does not use the full screen as it would with "match_parent".

MainActivity.java:

public class MainActivity extends Activity {

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);     

        LinearLayout fragContainer = (LinearLayout) findViewById(R.id.llFragmentContainer);

        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);

        ll.setId(12345);

        getFragmentManager().beginTransaction().add(ll.getId(), TestFragment.newInstance("I am frag 1"), "someTag1").commit();
        getFragmentManager().beginTransaction().add(ll.getId(), TestFragment.newInstance("I am frag 2"), "someTag2").commit();

        fragContainer.addView(ll);
    }
}

TestFragment.java:

public class TestFragment extends Fragment {

    public static TestFragment newInstance(String text) {

        TestFragment f = new TestFragment();

        Bundle b = new Bundle();
        b.putString("text", text);
        f.setArguments(b);
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v =  inflater.inflate(R.layout.fragment, container, false);

        ((TextView) v.findViewById(R.id.tvFragText)).setText(getArguments().getString("text"));     
        return v;
    }
}

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rlMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <LinearLayout
        android:id="@+id/llFragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="19dp"
        android:orientation="vertical" >
    </LinearLayout>
</RelativeLayout>

fragment.xml:

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="200dp" >

    <TextView
        android:id="@+id/tvFragText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="" />

</RelativeLayout>

And this is the result of the above code: (the two Fragments are aligned next to each other)

You are quite welcome I suppose :)

这篇关于如何将片段添加到程序生成的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆