Java 片段与视图

Java 片段与视图,java,android,Java,Android,我的应用程序分为多个片段,每个片段都有自己的布局(.xml文件) 当我开始在活动的onCreate()方法中可视化第一个片段时,我使用setContentView(fragment\u first)设置适当的布局。例如,如何更改第二个片段(fragment_second)中包含的TextView。一般来说,片段,或任何活动或视图都应该更新其自身的内部UI控件。它更简单,设计也很好。其他类/事件可能会更新片段的数据状态,但它会处理该状态的显示方式 编辑以回答已评论的问题: 以下是如何在片段中加载内

我的应用程序分为多个片段,每个片段都有自己的布局(.xml文件)


当我开始在活动的
onCreate()
方法中可视化第一个片段时,我使用
setContentView(fragment\u first)
设置适当的布局。例如,如何更改第二个片段(fragment_second)中包含的
TextView

一般来说,
片段
,或任何
活动
视图
都应该更新其自身的内部UI控件。它更简单,设计也很好。其他类/事件可能会更新
片段的数据状态,但它会处理该状态的显示方式

编辑以回答已评论的问题:

以下是如何在片段中加载内容视图:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.pump_info, container, false);
    return view;
}

似乎您为活动中的第一个片段设置了预期的xml文件。 简而言之,您应该创建一个全新的类,并让它扩展android.support.v4.app.Fragment
,同时让您的活动扩展FragmentActivity,而不仅仅是activity

然后在你的
android.support.v4.app.Fragment
类(从现在起我将称之为你的片段)中,你应该覆盖
onCreateView(LayoutFlater充气,ViewGroup容器,Bundle savedInstanceState){}
方法,在这个方法中你应该放一行如下:
View-View=inflater.inflate(R.layout.the_xml_layout_for_this_fragment,container,false),用于膨胀片段的布局并将其放置在活动布局中的适当位置

在此之后,您需要
返回视图
,但在返回此视图之前,可以执行
view.findViewById(来自\u xml\u布局文件的\u a\u视图的R.id\u)以查找元素并对其进行操作

您应该为应用程序中需要的每个片段创建这样的片段类,并让它膨胀自己的xml布局文件

有关更详细的说明,您可以查看其他视频或书面教程

编辑:下面是一个基本片段类:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyFrag extends Fragment {

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

    // inflate the view:
    View view = inflater.inflate(R.layout.myfrag_layout, container, false);

    // manipulate widgets, for example:
    TextView textView = (TextView) view.findViewById(R.id.textView);
    textView.setText("read me!!!");

    // return the view:
    return view;
    }

}
这是育儿活动:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

public class MyFragmentActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // notice that there is a different layout file for the activity and for
    // the fragment!
    setContentView(R.layout.xml_layout_for_the_activity);

    // to start the fragment and stick it into your activity (not needed if
    // you use ViewPager)
    FragmentManager fragMan = getSupportFragmentManager();
    fragMan.beginTransaction()
        .add(R.id.the_visual_element_that_will_contain_your_fragments_layout, fragMan)
        .commit();
    }

}
onCreateView()
方法中,应该将以后需要访问的所有视图分配给fragment类中的字段,如下所示

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

    this.aTextView = (TextView)view.findViewById(R.id.a_text_view);
    this.anImageView = (ImageView)view.findViewById(R.id.an_image_view);
    ...
}

然后,您可以在执行片段的其他位置使用
aTextView
,等等。

好的,我如何为该片段创建一个类并绑定它,以便我在片段上时可以加载该布局?如果这个答案有帮助,您应该将其标记为答案。如果你在别人花时间帮助你的时候不接受有效的答案,人们将来会不愿意帮助你。顺便说一句,你不必打电话给你的碎片经理碎片人,我这么做是因为这很有趣。不过,我不会告诉你我如何称呼我的资产经理。呵呵。。。XP