Fragment使用中的一些疑惑。
来源:互联网 发布:原画梦的网络课很烂 编辑:程序博客网 时间:2024/05/22 07:06
我在学习Android第一行代码的时候按照书上第四章的代码练习的时候遇到一些疑惑。首先贴上主布局:
<?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"> <fragment android:id="@+id/left_fragment" android:layout_width="0dp" android:layout_height="match_parent" android:name="com.example.leslie.fragmenttest.LeftFragment" android:layout_weight="1"/> <!--<FrameLayout android:id="@+id/right_layout" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" >--> <fragment android:id="@+id/right_fragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:name="com.example.leslie.fragmenttest.RightFragment" /></LinearLayout>
可以看出主布局很简单,就是一个线性布局下2个Fragment.下面贴出主活动中的代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button=(Button)findViewById(R.id.button); button.setOnClickListener(this); } public void onClick(View view){ switch (view.getId()){ case R.id.button: AnotherRightFragment fragment=new AnotherRightFragment(); FragmentManager fragmentManager=getFragmentManager(); FragmentTransaction transaction=fragmentManager.beginTransaction(); transaction.replace(R.id.right_fragment,fragment); transaction.commit(); break; default:break; }
代码的逻辑也很简单,就是希望点击左边的fragment中的按钮,能够更换右边的fragment。为此制作了右边的2个fragment布局文件如下
这是right_fragment布局文件:
<?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" android:background="#00ff00"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textSize="20sp" android:text="This is right fragment"/></LinearLayout>
这是another_right_fragment布局文件:
<?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:background="#ffff00" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textSize="20dp" android:text="This is another right fragment"/></LinearLayout>
两者并无太大不同。仅仅显示的text不同而已,运行结果并没有像我想象的那样右边的fragment完全覆盖住,而是仅仅覆盖了非TextView部分。
如图所示:
这让我百思不得其解。
网上搜索到的答案是用FrameLayout做被替代的fragment的父容器就可以了。我自己暂时还不知道为什么要这么做。为什么不用FrameLayout只能替代一部分导致还有一部分可见。
0 0
- Fragment使用中的一些疑惑。
- Epoll 使用中的疑惑
- vector使用中的一个疑惑
- 第一遍自学C#过程中的一些疑惑
- 关于PHP学习过程中的一些疑惑
- HSV 空间在OPENCV 中的一些疑惑
- 关于Unity中的_MainTex_ST的一些疑惑
- Java学习中的一些疑惑解答
- 一些疑惑
- C#中的一些疑惑(网摘整理)
- 关于编程过程中的一些疑惑及问题
- Android中的Fragment使用
- Fragment中的toolbar使用
- thymeleaf中的fragment使用
- ViewPager+Fragment使用中的Fragment生命周期
- 编译器的一些疑惑
- 一些疑惑问题
- java基础----一些疑惑
- linux学习:ls命令说明
- 印刷机输纸故障分析
- Ubuntu 设置内容丢失的问题
- Oracle数据库中调用Java类开发存储过程、函数的方法
- 关于苹果手机的真机调试
- Fragment使用中的一些疑惑。
- effective c++读书笔记 条款2
- deep-residual-networks
- jquery全局hover方法,支持全局调用
- java基本数据类型变量的赋值详解
- android 内存管理机制
- Windows系统常用目录(win7)
- 添加类库应用出现黄叹号
- 线程对象