<?xml version="." encoding="utf-"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:background="#f">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="潘侯爷"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="碧空海"/>
</LinearLayout>
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by panchengjia on //.
*/
public class LeftFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//通过参数中的布局填充获取对应布局
View view =inflater.inflate(R.layout.left_layout,container,false);
return view;
}
@Override
public void onPause() {
super.onPause();
}
}
<?xml version="." encoding="utf-"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.administrator.fragmenttest.MainActivity">
<fragment
android:layout_width="dp"
android:layout_height="match_parent"
android:layout_weight=""
android:name="com.example.administrator.fragmenttest.LeftFragment"
tools:layout="@layout/left_layout" />
<fragment
android:layout_width="dp"
android:layout_height="match_parent"
android:layout_weight=""
android:name="com.example.administrator.fragmenttest.RightFragment"
tools:layout="@layout/right_layout" />
</LinearLayout>
<?xml version="." encoding="utf-"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.administrator.fragmenttest.MainActivity">
<fragment
android:layout_width="dp"
android:layout_height="match_parent"
android:layout_weight=""
android:name="com.example.administrator.fragmenttest.LeftFragment"
tools:layout="@layout/left_layout" />
<FrameLayout
android:id="@+id/right"
android:layout_width="dp"
android:layout_height="match_parent"
android:layout_weight=""></FrameLayout>
</LinearLayout>
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
//声明本次使用到的java类
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
RightFragment rightFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*在activity对应java类中通过getFragmentManager()
*获得FragmentManager,用于管理ViewGrop中的fragment
* */
fragmentManager=getFragmentManager();
/*FragmentManager要管理fragment(添加,替换以及其他的执行动作)
*的一系列的事务变化,需要通过fragmentTransaction来操作执行
*/
fragmentTransaction = fragmentManager.beginTransaction();
//实例化要管理的fragment
rightFragment = new RightFragment();
//通过添加(事务处理的方式)将fragment加到对应的布局中
fragmentTransaction.add(R.id.right,rightFragment);
//事务处理完需要提交
fragmentTransaction.commit();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有