LinearLayout中动态添加Fragment不能填充整个Activity的问题
来源:互联网 发布:在淘宝如何找店铺 编辑:程序博客网 时间:2024/05/16 05:27
本人在学习Android开发中,关于Fragment这一块一直头疼不已,在使用LinearLayout做容器动态添加Fragment的时候常常无法根据已经设定的属性布局,例如:
XML布局文件:
1.activity_main.xml
<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="match_parent" tools:context=".MainActivity" android:orientation="horizontal" android:id="@+id/linearLayout" ></LinearLayout>2.first_fragment.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="0dp" android:layout_height="match_parent" android:background="@android:color/holo_orange_dark" android:layout_weight="1" android:id="@+id/first_layout" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="111111"/></LinearLayout>3.second_fragment.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="0dp" android:layout_height="match_parent" android:background="@android:color/holo_blue_dark" android:layout_weight="1" android:id="@+id/second_layout" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="2222"/></LinearLayout>
JAVA文件:
1.MainActivity.java
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager manager = getSupportFragmentManager(); FirstFragment firstFragment = new FirstFragment(); SecondFragment secondFragment = new SecondFragment(); manager.beginTransaction().add(R.id.linearLayout, firstFragment).commit(); manager.beginTransaction().add(R.id.linearLayout, secondFragment).commit(); }2.FirstFragment.java
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.first_fragment, null);}
3.SecondFragment.java
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.second_fragment, null );}
编译运行后,界面却是这样的:
本来在两个Fragment的layout中已经设置了权重,但是却在Activity中没有任何效果。后来经过查找资料,发现是FirstFragment.java和SecondFragment.java中onCreateView方法的问题,在android官方查阅文档如下:
-----------
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
Inflate a new view hierarchy from the specified xml resource. Throws InflateException
if there is an error.
Parameters
R.layout.main_page
)Returns
- The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.
当attachToRoot传入false值时,ViewGroup root用来保存布局参数,因此若像之前那样传入null,在xml文件中设置的布局参数都会丢失,android会采用默认的布局参数。
如果不相信的话,请运行sdk目录下tools/hierarchyviewer.bat,编译运行你的程序,在Hierarchy Viewer中找到该程序的包,双击。找到两个Fragment布局中的任意一个,比如id为first_layout的那个,点击查看其布局参数值:
请注意画红圈的部分,我们设置的match_parent,,layout_weight全部都失效,而是android自动采用了WRAP_CONTENT,说明我们配置的布局参数并没有被保存使用
现在,根据以上分析,重新配置两个Fragment.java文件,使用container和false值来保存布局参数,例如:
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.first_fragment, container, false);}再次编译运行:
问题得到解决,Fragment按照我们配置的布局参数填充了Activity。
PS:
本人只是android开发的初级学习者,遇到这个问题时在网上找不到任何解决办法,所以把自己解决的方式发出来,如果哪里有错误、不足的地方欢迎各位高手指出,帮助我提高,谢谢。
- LinearLayout中动态添加Fragment不能填充整个Activity的问题
- LinearLayout中动态添加Fragment不能填充整个Activity的问题
- activity中fragment不能全屏的问题
- Activity中动态添加Fragment.
- 在Activity中静态、动态添加Fragment
- Activity动态添加fragment
- Activity动态添加Fragment
- 动态填充LinearLayout ,LinearLayoutForListView
- Fragment(2)-在Activity中动态添加删除
- Android-Fragment(2)-在Activity中动态添加删除
- Android中使用ViewPager+Fragment动态删除添加Fragment问题
- 动态的添加ImageView到LinearLayout中并居中显示
- fragment中添加fragment与activity中添加fragment的不同
- Android Fragment的动态添加,Fragment,Activity以及Fragment之间的通信方式
- viewpager+fragment嵌套viewpager图片广告导致广告图片不能滑动的问题用LinearLayout实现
- 在Activity中添加Fragment
- Fragment系列—Activity中添加fragment
- Android中利用LinearLayout动态添加控件
- easyui学习笔记1:搭建环境,编写hellworld
- cocos2d-js游戏设计常用函数笔记
- 中国互联网金融协会挂牌在即
- 十月下旬腾讯,网易游戏,百度迅雷校园招聘笔试题集锦(第271-330题)
- 专注做事,竟然也成为我们的稀缺能力
- LinearLayout中动态添加Fragment不能填充整个Activity的问题
- LeetCode-32 Longest Valid Parentheses
- C++中关于数组与指针的几处难点
- 只做正确的事情,并持续输出价值
- iOS开发中如何向svn服务器commit添加.a文件
- Spring学习笔记五(JDKProxy和cglibProxy的区别)
- DevExpress v15.1:ASP.NET WebForms功能增强(三)
- Java 防止表单重复提交
- 解决Ubuntu系统中文乱码显示问题