7

我想在我的主要活动中添加一个片段,所以我有这个片段类(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 似乎表明它与片段视图膨胀有关,但我不确定。

顺便说一句,这个片段的原因是每个页面上都存在应用程序徽标(图像和一些可更新的文本),这是做类似事情的好方法吗?对不起我的新手问题。

4

5 回答 5

12

我已经从我的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 支持低版本操作系统中的片段。没有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;
}
于 2013-02-19T04:25:45.267 回答
4

在片段部分添加

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 />

于 2019-04-09T02:39:58.727 回答
0

确保具有activity_main.xml布局的活动扩展FragmentActivity而不是Activity

于 2013-02-19T04:24:52.747 回答
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();
}
于 2017-03-04T19:21:38.677 回答
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();
}
于 2019-01-08T06:23:33.677 回答