如何将Android片段添加到活动中?

时间:2013-02-19 04:09:35

标签: android xml layout android-activity fragment

我想在我的主要活动中添加一个片段,所以我有这个片段类(TitleFragment.java):

package com.myapp.app1;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TitleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_title, container, false);
    }
}

接下来是我的片段的实际内容,包含在fragment_title.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
       <ImageView 
        android:id="@+id/logo_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo_main"
        android:contentDescription="@string/app_name"
        android:scaleType="fitStart"
        android:adjustViewBounds="true" />
</LinearLayout>

在我的activity_main.xml内,我在活动的常规内容中有这个片段:

<fragment 
            android:name="com.myapp.app1.TitleFragment"
            android:id="@+id/title_fragment"
            android:layout_width="0dp"
            android:layout_height="match_parent" />

这种方法是否是创建片段的正确方法?我的应用程序崩溃了,LogCat似乎表明它与扩展片段视图有关,但我不确定。

顺便说一下,这个片段的原因是每个页面上都有app徽标(图像和一些可更新的文本),这是一个很好的方法吗?对不起我的新手问题。

5 个答案:

答案 0 :(得分:8)

我从MainActivity开始片段化。主要活动延伸FragmentActivity。我使用的方式是:

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.body_frame, new MyFragment()).commit();

在您的情况下,它应该看起来像:

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.title_fragment, new TitleFragment()).commit();

请记住我使用了FragmentActivity来启动Fragment。我还使用了android-support-v4.jar 在较低版本的OS中支持片段。没有android-support-v4.jarFragmentManager manager = getSupportFragmentManager();可能如下所示:FragmentManager manager = getFragmentManager();

<强>编辑:

你应该修改你的片段类:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view =  inflater.inflate(R.layout.fragment_title, container, false);
    // you can use findViewById() using the above 'view'
      ......................
      ....your code........
       ................
    return view;
}

答案 1 :(得分:0)

确保activity_main.xml布局的活动延伸FragmentActivity而不是Activity

答案 2 :(得分:0)

public void onMenuItemClick(View clickedView, int position) {
    switch (position){
        case 1:
             fragmentChanger("Notifications",new notifyFragment());
           break;
        case 2:
             fragmentChanger("QR Code",new qrFragment());
            break;
        case 3:
            fragmentChanger("User ID",new MainFragment());
            break;
        case 4:
            fragmentChanger("Settings",new settingsFragment());
            break;
        default:
            Toast.makeText(this, "Clicked on position: " + position, Toast.LENGTH_SHORT).show();
            break;
    }
}

//function that does the trasaction when a fragment object is passed
//R.id.container is my default frame
public void fragmentChanger(String title, Object expectedFragment){
   // mToolBarTextView.setText(title);
    transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.container, (Fragment) expectedFragment);
    transaction.addToBackStack(null);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    transaction.commit();
}

答案 3 :(得分:0)

使用标签,您可以在活动中添加/替换片段

private final static String BLANK_FRAGMENT_A= "BlankFragmentA";
private final static String BLANK_FRAGMENT_B= "BlankFragmentB";

private BlankFragmentA blankFragmentA;
private BlankFragmentB blankFragmentB;

添加片段

blankFragmentA= (BlankFragmentA) getSupportFragmentManager().findFragmentByTag(BLANK_FRAGMENT_A);
if (blankFragmentA== null) {
   blankFragmentA= new BlankFragmentA ();
   getSupportFragmentManager().beginTransaction().add(R.id.frame_layout,blankFragmentA,BLANK_FRAGMENT_A).commit();
}

替换片段

blankFragmentB= (BlankFragmentB) getSupportFragmentManager().findFragmentByTag(BLANK_FRAGMENT_B);
if (blankFragmentB== null) {
   blankFragmentB= new BlankFragmentB();
   getSupportFragmentManager().beginTransaction().replace (R.id.frame_layout,blankFragmentB,BLANK_FRAGMENT_B).commit();
}

答案 4 :(得分:0)

在片段中添加

tools:layout="@layout/fragment_title

喜欢


<fragment 
    android:name="com.myapp.app1.TitleFragment"
    android:id="@+id/title_fragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    tools:layout="@layout/fragment_title />