在Fragment Activity中动态包含另一个布局

这是我的Home.xml布局文件

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rellay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@drawable/placesgradient">

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="click" 
        />

    **<I want to include another layout here on a click of a button dynamically on click of a button >**

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
       android:layout_below="@id/btn2"
        android:text="hello this is text..see above" />

</RelativeLayout>

这是我的Home.java文件

public class Home extends Fragment implements OnClickListener {
    Button b1;
    RelativeLayout r1;
    View rootView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.home, container, false);
        b1 = (Button) rootView.findViewById(R.id.btn2);
        b1.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onClick(View arg0) {
        ViewGroup con = null;
        r1 = (RelativeLayout) rootView.findViewById(R.id.rellay);
        LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
        r1.addView(layoutInflater.inflate(R.layout.test1, con , false));

    }
}

当我单击按钮时,它将test1.xml布局文件放置在home.xml布局的顶部。我需要它在按钮和文本之间。我之前在堆栈上检查了此代码,但似乎无法与FragmentActivity一起使用。在这种情况下,此处1表示索引或第二个子级。

rl.addView(1, layoutInflater.inflate(R.layout.content_layout, this, false) );

我该如何实现?帮助!!

萨维尔·卡佩勒(Xaver Kapeller)

我将一些容器视图放在您希望布局像这样的位置:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/rellay"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:background="@drawable/placesgradient">

    <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="click"
            />

    <FrameLayout 
            android:id="@+id/flContainer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_below="@id/btn2"
            android:text="hello this is text..see above" />

</RelativeLayout>

然后,您可以像这样在Fragment中检索容器:

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

    // Retrieve your container
    flContainer = rootView.findViewById(R.id.flContainer);

    b1 = (Button) rootView.findViewById(R.id.btn2);
    b1.setOnClickListener(this);

    return rootView;
}

然后,当您想添加布局时,可以FrameLayout像这样添加一个子级

flContainer.addView(subLayout);

如果以后要更改容器中的布局,则可以这样进行:

flContainer.removeAllViews();
flContainer.addView(otherLayout);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Fragment Activity中动态包含另一个布局

来自分类Dev

BroadcastReceiver为了将数据从另一个Activity传递到Fragment

来自分类Dev

另一个片段内的Map Fragment导致Activity崩溃

来自分类Dev

Fragment 接口/侦听器适用于 Activity,但不适用于另一个 Fragment

来自分类Dev

使用另一个类的 Map Fragment

来自分类Dev

Fragment中的上下文菜单使用另一个Fragment中的ListView:registerForContextMenu(getListView())

来自分类Dev

如何在另一个Fragment中更新ListView?

来自分类常见问题

我在尝试从另一个片段中的Fragment调用函数时得到NullPointerException

来自分类Dev

导航回到从另一个活动中调用Fragment

来自分类Dev

尝试从另一个类实例化方法时,Fragment中的NullPointer异常

来自分类Dev

如何在另一个 Fragment 中退出 Google 身份验证?

来自分类Dev

如何从 Fragment 中的另一个 XML 文件使用 FloatingActionButton

来自分类Dev

将对象从Fragment的GridView传递到另一个Fragment的ListView

来自分类Dev

在两面板fragment Activity中,如何让一个fragment接管全屏?

来自分类Dev

在Fragment内以编程方式将视图添加到另一个视图

来自分类Dev

Android-从另一个Fragment类更新ListView

来自分类Dev

启动另一个片段后如何在Fragment中保存数据?

来自分类Dev

Android-从FragmentActivity到另一个Fragment的“后退”按钮

来自分类Dev

(android)如何命名一个Activity,Fragment..etc

来自分类Dev

Fragment创建一个Activity对象,但它为null

来自分类Dev

如何从 Fragment 打开一个新的 Activity?

来自分类Dev

如何调用一个Class(Tabfragment.java)到另一个Fragment(DeliveryTab.java)?

来自分类Dev

无法使用 Volley 库中的 Intent 从 Fragment 继续下一个 Activity

来自分类Dev

如何从包含合并标记的布局访问另一个布局中的视图?

来自分类Dev

我的 Fragment 布局与 Activity 布局重叠

来自分类Dev

从Fragment调用Activity然后返回Fragment

来自分类Dev

从另一个SearchActivity将字符串添加到SQLite数据库后,在Fragment中更新ListView

来自分类Dev

从Activity调用Fragment方法

来自分类Dev

从Activity调用Fragment方法

Related 相关文章

  1. 1

    在Fragment Activity中动态包含另一个布局

  2. 2

    BroadcastReceiver为了将数据从另一个Activity传递到Fragment

  3. 3

    另一个片段内的Map Fragment导致Activity崩溃

  4. 4

    Fragment 接口/侦听器适用于 Activity,但不适用于另一个 Fragment

  5. 5

    使用另一个类的 Map Fragment

  6. 6

    Fragment中的上下文菜单使用另一个Fragment中的ListView:registerForContextMenu(getListView())

  7. 7

    如何在另一个Fragment中更新ListView?

  8. 8

    我在尝试从另一个片段中的Fragment调用函数时得到NullPointerException

  9. 9

    导航回到从另一个活动中调用Fragment

  10. 10

    尝试从另一个类实例化方法时,Fragment中的NullPointer异常

  11. 11

    如何在另一个 Fragment 中退出 Google 身份验证?

  12. 12

    如何从 Fragment 中的另一个 XML 文件使用 FloatingActionButton

  13. 13

    将对象从Fragment的GridView传递到另一个Fragment的ListView

  14. 14

    在两面板fragment Activity中,如何让一个fragment接管全屏?

  15. 15

    在Fragment内以编程方式将视图添加到另一个视图

  16. 16

    Android-从另一个Fragment类更新ListView

  17. 17

    启动另一个片段后如何在Fragment中保存数据?

  18. 18

    Android-从FragmentActivity到另一个Fragment的“后退”按钮

  19. 19

    (android)如何命名一个Activity,Fragment..etc

  20. 20

    Fragment创建一个Activity对象,但它为null

  21. 21

    如何从 Fragment 打开一个新的 Activity?

  22. 22

    如何调用一个Class(Tabfragment.java)到另一个Fragment(DeliveryTab.java)?

  23. 23

    无法使用 Volley 库中的 Intent 从 Fragment 继续下一个 Activity

  24. 24

    如何从包含合并标记的布局访问另一个布局中的视图?

  25. 25

    我的 Fragment 布局与 Activity 布局重叠

  26. 26

    从Fragment调用Activity然后返回Fragment

  27. 27

    从另一个SearchActivity将字符串添加到SQLite数据库后,在Fragment中更新ListView

  28. 28

    从Activity调用Fragment方法

  29. 29

    从Activity调用Fragment方法

热门标签

归档