xml布局内容总结(一)--Android
2015-07-21 09:56
471 查看
关于安卓项目中xml的使用非常多,为了达到一些好的UI效果,需要对xml比较熟练,会使用很多的小技巧,本人准备对这些小技巧进行整理和总结,希望进行分享和交流。
关于weight的使用,由于weight在布局中主要按比例进行组件的摆放,因此比较容易解决适配的问题,所以学会使用weight会减轻开发过程中部分繁琐的开发任务。
1.使用weight居中显示
<?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"
android:gravity="center"
android:weightSum="1"
>
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Weight_Button" />
</LinearLayout>
![](http://img.blog.csdn.net/20150721100306751?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
2.使用weight实现相同宽度的n个组件(这里用3个做测试)
<?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"
android:weightSum="3"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Weight_Button1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Weight_Button2" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Weight_Button3" />
</LinearLayout>
![](http://img.blog.csdn.net/20150721100702261?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
3.使用weight实现不同比例宽度的n个组件(这里用4个做测试)
<?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"
android:weightSum="10"
>
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="4"
android:background="@color/yellow"
android:text="4" />
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="3"
android:background="@color/red"
android:text="3" />
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="2"
android:background="@color/yellow"
android:text="2" />
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:background="@color/red"
android:text="1" />
</LinearLayout>
![](http://img.blog.csdn.net/20150721105029979?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
占据比例分别为总宽度的0.4,0.3,0.2,0.1
关于weight的使用,由于weight在布局中主要按比例进行组件的摆放,因此比较容易解决适配的问题,所以学会使用weight会减轻开发过程中部分繁琐的开发任务。
1.使用weight居中显示
<?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"
android:gravity="center"
android:weightSum="1"
>
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Weight_Button" />
</LinearLayout>
2.使用weight实现相同宽度的n个组件(这里用3个做测试)
<?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"
android:weightSum="3"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Weight_Button1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Weight_Button2" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Weight_Button3" />
</LinearLayout>
3.使用weight实现不同比例宽度的n个组件(这里用4个做测试)
<?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"
android:weightSum="10"
>
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="4"
android:background="@color/yellow"
android:text="4" />
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="3"
android:background="@color/red"
android:text="3" />
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="2"
android:background="@color/yellow"
android:text="2" />
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:background="@color/red"
android:text="1" />
</LinearLayout>
占据比例分别为总宽度的0.4,0.3,0.2,0.1
相关文章推荐
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
- [Android]使用draw9patch制作可拉伸图片
- android通过Location API显示地址信息的实现方法
- Android开发者学习必备:10个优质的源码供大家学习
- 第三波精品Android源码袭来!免费下载
- Android研究院之应用程序ListView 详解 (六)
- Android典型界面设计——FragmentTabHost+Fragment实现底部tab切换
- ProtocolBuffer在Android端的解析
- AndroidJNI.SetStaticBooleanField设置静态布尔域
- Android Binder详解
- 在线教育遭遇瓶颈 扣丁领衔O2O时代
- GitHub 优秀的 Android 开源项目
- Android通过反射打造可以存储任何对象的万能SharedPreferences
- AndroidJNI.SetStaticBooleanField设置静态布尔域
- android的ndk学习(1)
- Android开发之获取设备的屏幕信息
- Android提高(12)——第二章 Handler怎么用
- Android实现返回拍摄的图片功能实例
- Android基础入门教程——2.2.6 AbsoluteLayout(绝对布局)
- Android基础入门教程——2.2.5 GridLayout(网格布局)