我在看《第一行代码》中的碎片部分,对其中的这部分代码没有办法理解
<?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="horizontal">
<fragment
android:id="@+id/left_fragment"
android:name="com.example.qiao.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
<FrameLayout
android:id="@+id/right_layout"
android:layout_width="10dp"
android:layout_weight="1"
android:layout_height="match_parent">
<fragment
android:id="@+id/right_fragment"
android:name="com.example.qiao.fragmenttest.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</FrameLayout><!--相对布局-->
</LinearLayout>
我的主要困惑是不理解这里为什么要添加FrameLayout
,作用是覆盖在fragment
上,类似于浮动的效果吗?
同时
<FrameLayout
android:id="@+id/right_layout"
android:layout_width="10dp"
android:layout_weight="1"
android:layout_height="match_parent">
这部分属性到底作用在哪里又该怎么理解,如何确定他们的大小。
怪我咯2017-04-17 14:56:43
There are two ways to host a UI Fragment in an Activity: (1) add the fragment to the activity layout; (2) add the fragment in the activity code.
left_fragment
and right_fragment
both use the first method.
For the second method, you need to add a container for Fragment in the layout file to arrange the position of Fragment in the activity view. In practice, FrameLayout
is often used as a container.
As for the function of the right_layout
you posted, it depends on the intention of the code. I guess the author wants to demonstrate how to add a Fragment through code (that is, the second method) to compare with the first method. Or it might not mean anything because the layout file is wrong!
The first error needs to be corrected. In the subcomponent of the FrameLayout view, the layout_weight attribute is invalid , and you will get the following yellow exclamation mark warning:
Invalid layout param in a FrameLayout: layout_weight
According to the layout file you posted, right_fragment will not be displayed at all because layout_width is 0dp.
The second error needs to be corrected. is a comment error. FrameLayout is not a relative layout. It is a container. Any subcomponent added to it needs to be determined by the layout_gravity
attribute value in the parent. The position in the view, and each subcomponent cannot define relative positions like RelativeLayout.
This property tells LinearLayout how to arrange the layout of child components. For a horizontally oriented LinearLayout, look at layou_width
and layout_weight
to determine the width of the child component.
In the layout you posted, LinearLayout has 2 sub-components. Hereinafter, left_fragment is called child1 and right_layout is called child2:
Step 1, check the layout_width attribute value, assign 0dp to child1 and 10dp to child2;
Step 2, allocate the remaining width according to the layout_weight attribute value, child1 will be allocated to 2/3, child2 will be allocated to 1/3.
迷茫2017-04-17 14:56:43
Have you seen the orientation of the outermost LinearLayout, and the width of the FrameLayout is only 10dp. :D