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)

Added in API level 1

Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.

Parameters
resourceID for an XML layout resource to load (e.g., R.layout.main_page)rootOptional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)attachToRootWhether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.
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开发的初级学习者,遇到这个问题时在网上找不到任何解决办法,所以把自己解决的方式发出来,如果哪里有错误、不足的地方欢迎各位高手指出,帮助我提高,谢谢。

0 0
原创粉丝点击