控件布局初步(一)
1. 空间布局的基本概念
2. 空间布局的种类
3. 各类布局的特点
4. 线性布局的基本方法
1. 空间布局的基本概念
大致分为2类:
<1>使用布局文件完成空间布局 使用起来方便, 有可视化布局
<2>在Java代码中完成控件布局 可灵活、动态布局
2. 空间布局的种类
<1>Linear Layout 比较简单, 适合初学
Relative Layout 实际中使用更广泛
<2>ListView 也非常广泛
3. 各类布局的特点
4. 线性布局的基本方法
首先学习Linear layout, 默认fargment是Realative layout, 因此要自己再Layout文件夹右键New andorid xml File
first_lay.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > //垂直方向上摆放 6 7 <TextView 8 android:layout_width="match_parent" //在activity中就会macth activity 9 android:layout_height="wrap_content" //只是包裹了文字内容 10 android:textColor="#FF0000" 11 android:text="NO1_TextView" /> 12 13 <TextView 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:textColor="#00FF00" 17 android:text="NO2_TextView" /> 18 19 </LinearLayout>
保存后可以在可视化窗口预览情况
接下来需要在加载时加载first_lay而不是之前的fragment_main.xml即可
MainActivity.java
1 public static class PlaceholderFragment extends Fragment { 2 3 public PlaceholderFragment() { 4 } 5 6 @Override 7 public View onCreateView(LayoutInflater inflater, ViewGroup container, 8 Bundle savedInstanceState) { 9 View rootView = inflater.inflate(R.layout.first_lay, container, false); 10 return rootView; 11 } 12 }
运行情况