用权重来实现屏幕自适应

来源:互联网 发布:怎么ping端口号 编辑:程序博客网 时间:2024/05/01 21:48

用权重来实现屏幕自适应

一直以来没能有时间去整理屏幕自适应的问题,今天特地整理了一部分。
-安卓手机的屏幕大小种类很多,安卓的屏幕自适应就显得非常重要了


首先直接上代码

代码块

新建一个项目Test_screen,在其布局文件activity_main.xml中的代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"         android:orientation="horizontal"        android:background="#ff00ff">        <View             android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:background="#00ef00"            />         <View             android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:background="#ffef00"            />          <View             android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:background="#00efff"            />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"         android:orientation="vertical"         android:background="#0000ff">          <View               android:layout_width="match_parent"              android:layout_height="0dp"              android:layout_weight="1"              android:background="#afa10f"              />           <View               android:layout_width="match_parent"              android:layout_height="0dp"              android:layout_weight="1"              android:background="#ffdc0e"              />            <View               android:layout_width="match_parent"              android:layout_height="0dp"              android:layout_weight="1"              android:background="#94ba0e"              />    </LinearLayout></LinearLayout>
代码解析:首先在手机上放置两个控件,设置权重都是1,特别说明一下,android:layout_weight设置权重最好用  android:layout_width="0dp"或者 android:layout_height="0dp"来匹配,当然 android:layout_height设置为match_parent也是可以的,但是匹配起来的方法就会有很多种方法了,建议把控件的高或者宽设置为0。通过设置宽或高是0dp就可以使控件成正比例显示。程序运行结果如下图:![程序运行结果如下图](http://img.blog.csdn.net/20151026132905158)----------

对安卓不是很熟悉,如有什么错误或者有更好的建议欢迎评论予以指正!谢谢!


0 0
原创粉丝点击