您的位置:首页 > 移动开发 > Android开发

android布局与多屏适配---layout_weight

2013-01-01 16:43 246 查看
android中有的屏幕尺寸不一,分辨率不同,这就该开发带来了适配的问题。这里分享其中一种方式,使用layout_weight。

1.android中使用的单位:推荐在android中使用dip(dp) 替代px, 使用sp 表示字体大小。具体的有关dip,px dpi 之间的关系,转换,可以参照其他网友提供的方法。

效果图:


x
基本原理就是把layout_width layout_height 重新封装成四个style,结合layout_weight 使用,确定在水平或者是垂直方向上占用 的比例。这样,无论屏幕是变大还是变小,控件在空间上占用的比例是固定的,即使拉伸或压缩,也不会有太多的变形。

重新封装的layout_width 和layout_height 代码为:

<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
        style="@style/layout_vertical"
        android:layout_weight="1"
        android:orientation="horizontal" >
        <View
            style="@style/layout_horizontal"
            android:layout_weight="1"
            android:background="#ff0000" />
        <View
            style="@style/layout_horizontal"
            android:layout_weight="4"
            android:background="#00ff00" />
        <View
            style="@style/layout_horizontal"
            android:layout_weight="2"
            android:background="#000080" />
        <View
            style="@style/layout_horizontal"
            android:layout_weight="3"
            android:background="#808000" />
    </LinearLayout>
    <LinearLayout
        style="@style/layout_vertical"
        android:layout_weight="2"
        android:orientation="vertical" >
        <View
            style="@style/layout_vertical"
            android:layout_weight="1"
            android:background="#808080" />
        <View
            style="@style/layout_vertical"
            android:layout_weight="2"
            android:background="#808000" />
        <View
            style="@style/layout_vertical"
            android:layout_weight="3"
            android:background="#0000ff" />
        <View
            style="@style/layout_vertical"
            android:layout_weight="4"
            android:background="#ffff00" />
    </LinearLayout>
</LinearLayout>


这里面使用了三个linearlayout,最外面的垂直方向的linearlayout,被分成两部分,内部的第一个linearlayout为水平方向,比重为1,第二个占的比重 为2,为垂直方向。

下面贴一个在实际的使用的例子。

效果图:


布局文件代码如下:

<?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:background="@drawable/common_bg"
    android:orientation="vertical" >
    <LinearLayout
        style="@style/layout_vertical"
        android:layout_weight="1"
        android:background="@drawable/common_top"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/reverse_btnToBack"
            style="@style/layout_horizontal"
            android:layout_marginLeft="5dp"
            android:layout_weight="3"
            android:background="@drawable/common_back"
            android:text="返回" />
        <View
            style="@style/layout_horizontal"
            android:layout_weight="1" />
        <TextView
            style="@style/layout_horizontal"
            android:layout_weight="2"
            android:gravity="center"
            android:text="预定"
            android:textColor="#ffffff"
            android:textSize="18sp" />
        <View
            style="@style/layout_horizontal"
            android:layout_weight="1" />
        <Button
            android:id="@+id/reverse_btnRefresh"
            style="@style/layout_horizontal"
            android:layout_weight="2"
            android:background="@drawable/my_comment"
            android:text="刷新" />
        <View
            style="@style/layout_horizontal"
            android:layout_weight="1" />
    </LinearLayout>
    <LinearLayout
        style="@style/layout_vertical"
        android:layout_weight="1"
        android:gravity="center" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center|center_vertical"
            android:gravity="center"
            android:text="商品列表"
            android:textSize="24sp" />
    </LinearLayout>
    <LinearLayout
        style="@style/layout_vertical"
        android:layout_weight="4" >
        <ListView
            android:id="@+id/reverse_listView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/detail_content" >
        </ListView>
    </LinearLayout>
    <LinearLayout
        style="@style/layout_vertical"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/reverse_btnToCheck"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/tocheck"
            android:textSize="30sp" />
    </LinearLayout>
    <LinearLayout
        style="@style/layout_vertical"
        android:layout_weight="1" >
    </LinearLayout>
</LinearLayout>


第一次贴东西,希望能给大家带来帮助,有不清楚的地方,欢迎发问。有错误之处,敬请指教。

推荐大家参考 http://www.eoeandroid.com/forum.php?mod=viewthread&tid=173973&page=1#pid1485630
这个帖子说的比较详细。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: