【问题标题】:Is it possible to have two fragment xml in one activity xml?是否可以在一个活动 xml 中有两个片段 xml?
【发布时间】:2015-07-26 07:45:33
【问题描述】:

我正在尝试创建一个欢迎活动,其中包含标题片段 xml 和底部视图片段 xml,其中包含登录按钮。谁能帮助我如何将这两个片段 xml 放入我的activity_main.xml

这是我的header.xml

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

    <ImageView
        android:id="@android:id/background"
        android:layout_width="match_parent"
        android:layout_height="220dp"
        android:scaleType="centerCrop"
        android:src="@drawable/header_bg" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="bottom|right|end"
        android:layout_marginStart="@dimen/android_spaces_large"
        android:layout_marginEnd="@dimen/android_spaces_large"
        android:layout_marginLeft="@dimen/android_spaces_large"
        android:layout_marginRight="@dimen/android_spaces_large"
        android:layout_marginBottom="@dimen/android_spaces">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/header_title"
            style="@style/Header.TitleText" />

        <TextView
            android:id="@+id/subtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/header_subtitle"
            style="@style/Header.SubTitleText" />

    </LinearLayout>

    <View
        android:id="@android:id/title"
        android:layout_width="wrap_content"
        android:layout_height="?android:actionBarSize"
        android:background="@layout/header_ab_shadow" />



</FrameLayout>

还有我的fragment_bottomview.xml,它只包含一个按钮。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

而我的activity_main.xml 看起来像这样

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.windows81.welcomingwithheader.MainActivity"
    tools:ignore="MergeRootFrame" />

我不知道我缺少什么或者我做错了什么,任何教程链接都会真正帮助我。

【问题讨论】:

  • 使用相对布局。在顶部添加一个片段,相对于它你可以在它下面有另一个片段
  • 为什么需要片段?如果您不如何使用片段,则很可能不需要它们...
  • @Raghunandan 我会将 activity_main.xml 上的框架布局更改为相对布局?
  • @pskink 有什么方法可以实现标题并在其底部有按钮?
  • 请参阅developer.android.com/training/improving-layouts/…,了解如何在父布局 xml 文件中使用子布局 xml 文件

标签: java android xml android-fragments


【解决方案1】:

只需创建两个新类,我们称它们为 FragmentA 和 FragmentB,它们都应该扩展 Fragment

而不是覆盖fragmentA和fragmentB的onCreateView,并将它们的视图设置为您的xml到header.xml和fragment_bottomview.xml,如下所示

  public class FragmentA extends Fragment {

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

    return view;
}


}


public class FragmentB extends Fragment {

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

    return view;
}


}

现在要将 FragmentA 和 FragmentB 添加到您的活动中,将此代码放入您的 oncreate 活动方法中

    FragmentA fragmentA=new FragmentA();
    FragmentManager manager=getFragmentManager();
    FragmentTransaction trans=manager.beginTransaction();
    trans.add(R.id.container,fragmentA,"fragmenta");/*adding fragment into 
                                                       the 
                                                 framelayout with id   
                                                container*/
   FragmentB fragmentB=new FragmentB();
   trans.add(R.id.container,fragmentB,"fragmentb");/*adding fragment into 
                                                  the 
                                                 framelayout with id   
                                                container*/

    trans.commit();

你可能应该使用相对布局而不是框架布局

【讨论】:

  • 我将把最后一个代码放在哪里?在我的 MainActivity 上?或者在两个片段类上?如果我错了,请纠正我,我会将我的 activity_main.xml 上的框架布局更改为 relativelayout,对吧?
  • 将 MainActivity 上的最后一个代码放在 oncreate 方法中,并将 framelayout 更改为 relativelayout
  • @KeanPadua 你不需要片段来做你想做的事:查看你问题下方的 cmets
  • @KeanPadua as pskink 说您可能不需要片段,只需将它们放在相同的布局中,但无论如何我会考虑将片段用于学习目的,因为它将在未来对您有很大帮助
  • @pskink 是的。我解决了我的问题。我只是将 framelayout 更改为 relativelayout 并将 layout_height 更改为 match_parent ,这样我就可以在图像下方有一个按钮。就像你说的,我不需要这个片段。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-06
  • 2011-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多