
在onCreateView中返回fragment的视图
public class MyFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_test, null)
return view
}
}
方法一,静态的使用Fragment,直接在布局文件中加入fragment
android:name指定fragment的类
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name=".MyFragment"/>
</LinearLayout>
方法二,动态添加fragment,
activity布局,用一个framelayout作为fragment容器
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.demo.MainActivity" >
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</RelativeLayout>
activity代码
package com.example.demo
import android.app.Activity
import android.app.FragmentManager
public class MainActivity extends Activity {
FragmentManager fm
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
fm = getFragmentManager()
fm.beginTransaction().add(R.id.fragment_container,new MyFragment()).commit()
}
Eclipse ADT是创建手机Application的必备软件,那么如何在Eclipse ADT中创建并运行Application呢?下面我给大家分享一下。
工具/材料Eclipse ADT
首先打开Eclipse ADT软件,点击顶部的File菜单,然后选择New下面的Android Application Project选项,如下图所示
接下来在d出的New Android Application界面中编写Application的项目信息,如下图所示
然后会进入到选择项目图标的界面,这里注意勾选Create activity复选框,如下图所示
接下来会进入Activity类型选择界面,这里直接选择Blank Activity选项,如下图所示
然后我们需要给Activity起名字并设置布局名称,如下图所示
接下来回到ADT软件主界面我们就会看到Application项目已经创建好了,如下图所示
然后如果要运行项目的时候我们直接右键单击项目名称,然后选择Run As下面的Android Application选项,如下图所示
最后我们就可以看到ADT中自带的模拟器启动了,并且输出了程序中的信息,如下图所示
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)