Android中Fragment讲解及Fragment静态的使用

版权声明:文章内容系为本人原创,如需转载,请注明出处。 https://blog.csdn.net/weixin_43778720/article/details/87857106

一、Android中针对Fragment的定义:
Fragment是为用户提供动态的、多窗口交互体验的组件
二、静态Fragment的使用
1)创建一个Layout文件

<?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" >
 
    <TextView
        android:id="@+id/textView"
        android:textSize="20sp"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment示例" />
 
</LinearLayout>

2)创建一个类继承Fragment

public class StaticFragment extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {     
        return inflater.inflate(R.layout.static_fragment_layout, container, false);
    }
}

3)布局中声明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"
    >

    <fragment  android:name="com.bwie.www.bzh_day16_fragment.fragment.StaticFragment"
        android:id="@+id/myfragment_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         />
</RelativeLayout>

4)最终效果

public class MainActivity extends ActionBarActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = (TextView) findViewById(R.id.tv_static_fragment);
        tv.setText("床前明月光");
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43778720/article/details/87857106