If we want to achieve an effect of 1/3 on the left and 2/3 on the right as shown in the figure
At this time, you need to use Fragment
As can be seen from the figure, at least two layout files are required
i.e. left fragment (left_fragment.xml) and right fragment (right_fragment.xml)
left_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="match_parent" android:layout_height="match_parent" android:background="@color/colorAccent"> <Button android:id="@+id/button" android:layout_gravity="center_horizontal" android:text="This is button in left_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
right_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="match_parent" android:layout_height="match_parent" android:background="@color/colorPrimary"> <TextView android:id="@+id/textview" android:layout_gravity="center_horizontal" android:text="This is textview in left_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
It can be seen that there is only one button on the left and a text box on the right
As mentioned in the directory, Fragment is very similar to Activity, so if there are layout files, there should also be Class files.
We New a Java Class named LeftFragment
As shown in the figure, Fragment inherited from the v4 package
But some friends will ask, there are two packages on it, why use v4? Is it not built into the system?
Here's an explanation for you: Fragment of v4 can keep fragments functionally consistent across all Android system versions.
For example: For example, using Fragment nested in Fragment, this function is only supported in Android 4.2 system. If you use the built-in Fragment, your program will crash before 4.2. And with v4 there is no problem.
The white text on a red background is taken from the first line of code. I don't understand it very well, but I feel it is useful, so I put it up.
The Java class code is as follows
public class LeftFragment extends Fragment { // Rewrite onCreateView method @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { // The data type here is View , so just return inflater to return inflater.inflate(R.layout.left_fragment , container, false ); } }
Just override the onCreateView method
The same reason for the right fragment is not repeated here.
After the fragment is written, declare it in the main activity
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <!--注意,fragment必须要有id,否则无法编译--> <fragment android:id="@+id/left_fragment" android:name="com.loser.fragmenttext.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/right_fragment" android:name="com.loser.fragmenttext.RightFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" /> </LinearLayout>
可以看到,name属性写的是刚才定义的全路径包名
id属性一定要有。fragment标签必须要有id否则无法通过编译。
至此,一个简单的碎片静态加载已经完成。