Android各种Layout特性和使用汇总(一)
2012-02-08 15:34
351 查看
LinearLayout 线性布局
特性:横向或纵向,每行或每列(根据方向)只能有1个对象,可以设置对象在LinearLayout中占位的权重。
看上去很简单,也确实很简单,但LinearLayout却是Android中使用最多的布局之一,尤其是在多个Linear嵌套使用的时候,就会显得异乎寻常的强大。
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:text="Title" android:textSize="18dp" android:background="#AA3333FF" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ListView android:id="@+id/listViewInLinear" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingTop="5dp" android:paddingBottom="5dp" android:layout_weight="1" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show" android:layout_weight="50" /> <Button android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Back" android:layout_weight="50" /> </LinearLayout[b]>[/b] </LinearLayout[b]>[/b] |
1 2 3 4 5 6 7 | ListView arrowList=(ListView) findViewById(R.id.listViewInLinear); arrowList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings)); //mString定义 private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", ... } |
注意:同1个LinearLayout中可以设置多个子部件都有layout_weight属性,此时它们的大小会按weight的比例来进行分配,一般建议如果有多个weight,可以考虑让它们的和等于100。
另外,如果设置了layout_weight,那么与LinearLayout相同方向的大小设置不要设置fill_parent,而是设置wrap_content比较好,否则会发生问题。(具体情形遇见便知)
关键字:
orientation(方向),layout_weight(权重),嵌套
RelativeLayout 相对位置布局
特性:布局内对象的位置可以相对于Layout本身来定位,也可以根据之前的对象来定位。非常适合用来表现多个对象对齐(或者故意不对齐?)的布局。
上面这段描述像不像什么都没说?其实RelativeLayout可以做的事情基本上都可以由多个LinearLayout来实现,但在条件适合的时候用RelativeLayout来实现会更方便,更强大。而且有些情况下几乎只有RelativeLayout才能实现了。
考虑以下这个布局的实现方式,
布局的第1行其实完全可以用LinearLayout来实现,只需要添加1个无文本的TextView在伸展区,并且设置layout_weight即可,虽然有些麻烦,但仍然可以实现。但第2行如何实现呢?要求是新的按钮与Right1右对齐。而用RelativeLayout就可以轻松的实现,下面是Layout定义文件:
例子:
1 2 3 4 5 6 78 | <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/relaLeftButton" android:text="Left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> <Button android:id="@+id/relaRight2Button" android:text="Right2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" /> <Button android:id="@+id/relaRight1Button" android:text="Right1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@id/relaRight2Button" android:layout_toLeftOf="@id/relaRight2Button" /> <Button android:id="@+id/relaRight3Button" android:text="Right3Long" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@id/relaRight1Button" android:layout_below="@id/relaRight1Button" /> </RelativeLayout[b]>[/b] |
是不是达成要求了?虽然这是1个比较极端的例子,但在实际应用中,RelativeLayout的使用还是相当普遍的,通常用于登录画面之类的,元素不多,但要求布局相对宽松尽量利用空间的情况下。
相关文章推荐
- 教程:Android各种Layout特性和使用汇总
- Android各种Layout特性和使用汇总(二)
- Android各种Layout特性和使用(1)
- Android各种Layout特性和使用(2)
- android新特性:CoordinatorLayout与FloatingActionButton、Snackbar的使用
- android新特性:使用CollapsingToolbarLayout实现折叠效果及问题解决
- android新特性:使用CollapsingToolbarLayout实现折叠效果及问题解决
- android 基本布局(RelativeLayout、TableLayout等)使用方法及各种属性
- android 使用RelativeLayout布局出现的问题汇总(不定期添加更新)
- android新特性:DrawerLayout与NavigationView配合使用
- android新特性:DrawerLayout与NavigationView配合使用
- Android5.x新特性之Toolbar,AppBarLayout,CoordinatorLayout,CollapsingToolbarLayout等汇总
- android新特性:TextInputLayout使用方法
- 在AndroidManifest.xml文件中键盘和自适应的android:windowSoftInputMode属性使用 | keyboard,squeezing,layout
- android material design之Tablayout,Recyclerview,Fragment,Viewpager搭配使用(四)
- android:layout_weight padding margin 的使用
- Android FlexboxLayout的简单了解以及使用
- Android LayoutInflater.inflate使用上的问题解惑
- Android进阶之路 - CoordinatorLayout、AppBarLayout、CollapsingToolbarLayout、Toolbar的详细使用
- AndroidStudio使用问题汇总——导入(import)工程时常见错误