详解Android中weight的使用方法

 更新时间:2016年01月11日 09:20:43   作者:xu佳佳  
这篇文章主要向大家介绍了详解Android中weight的使用方法,感兴趣的小伙伴们可以参考一下

android中对weight的学习可以说是必须的,如果UI布局仅仅使用dp与sp等等,会让布局显得极度不灵活,毕竟各个手机屏幕大小不同,更别说是还有ipad之类的了,所以也是同做本人近期做的一个小UI来分享一下weight的使用。

    

  

左边是各个屏幕的显示效果,右边是1080*1920屏幕的具体显示效果。可以看到,不管屏幕如何变化,使用weight的布局中总是填充满屏幕的,至于美观效果就不说了,直接上代码。

小编使用的android studio,eclipse用户直接复制肯定会有问题,AS用户直接复制修改一下中间的图片便可以用啦。

<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"> 
 
  <RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.5" 
    android:background="#7EB345"> 
 
    <Button 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:background="@android:color/transparent" 
      android:drawableLeft="@drawable/left_menu" 
      android:paddingLeft="17dp" /> 
 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:text="某某科技大学" 
      android:textSize="25sp" /> 
 
    <Button 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_alignParentRight="true" 
      android:background="@android:color/transparent" 
      android:text="登陆" 
      android:textColor="#fff" 
      android:textSize="20sp" /> 
 
  </RelativeLayout> 
 
  <TextView 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1.5" 
    android:background="@drawable/school" /> 
 
 
  <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 
 
    <Button 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" 
      android:drawableTop="@mipmap/ic_launcher" 
      android:paddingTop="18dp" 
      android:text="校园新闻" /> 
 
    <Button 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" 
      android:drawableTop="@mipmap/ic_launcher" 
      android:paddingTop="18dp" 
      android:text="学术公告" /> 
 
    <Button 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" 
      android:drawableTop="@mipmap/ic_launcher" 
      android:paddingTop="18dp" 
      android:text="成绩查询" /> 
  </LinearLayout> 
 
  <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 
 
    <Button 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" 
      android:drawableTop="@mipmap/ic_launcher" 
      android:paddingTop="18dp" 
      android:text="课表信息" /> 
 
    <Button 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" 
      android:drawableTop="@mipmap/ic_launcher" 
      android:paddingTop="18dp" 
      android:text="图书借阅" /> 
 
    <Button 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" 
      android:drawableTop="@mipmap/ic_launcher" 
      android:paddingTop="18dp" 
      android:text="饭卡消费" /> 
  </LinearLayout> 
 
  <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 
 
    <Button 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" 
      android:drawableTop="@mipmap/ic_launcher" 
      android:paddingTop="18dp" 
      android:text="校园地图" /> 
 
    <Button 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" 
      android:drawableTop="@mipmap/ic_launcher" 
      android:paddingTop="18dp" 
      android:text="在线咨询" /> 
 
    <Button 
      android:id="@+id/neirongbuju" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" 
      android:drawableTop="@mipmap/ic_launcher" 
      android:paddingTop="18dp" 
      android:text="查看更多" /> 
  </LinearLayout> 
 
  </LinearLayout> 

以上就是本文的全部内容,希望对大家的学习有所帮助。

相关文章

  • Android TextView跑马灯效果实现方法

    Android TextView跑马灯效果实现方法

    这篇文章主要介绍了Android TextView跑马灯效果实现方法,涉及Android布局文件中相关属性的设置技巧,非常简单实用,需要的朋友可以参考下
    2016-01-01
  • android实现多线程下载文件(支持暂停、取消、断点续传)

    android实现多线程下载文件(支持暂停、取消、断点续传)

    本篇文章主要介绍了androids实现多线程下载文件,主要包括暂停、取消、断点续传等功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-02-02
  • Android实现简单图库辅助器

    Android实现简单图库辅助器

    这篇文章主要为大家详细介绍了Android实现简单图库辅助器的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • Android 存储路径选择方法

    Android 存储路径选择方法

    今天小编就为大家分享一篇Android 存储路径选择方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • android实现横屏的代码及思路

    android实现横屏的代码及思路

    android实现横屏的代码及思路,需要的朋友可以参考一下
    2013-05-05
  • Android实现3秒钟自动关闭界面

    Android实现3秒钟自动关闭界面

    这篇文章主要为大家详细介绍了Android实现3秒钟自动关闭界面,以支付成功为例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • Android四大组件之BroadcastReceiver详解

    Android四大组件之BroadcastReceiver详解

    今天小编就为大家分享一篇关于Android四大组件之BroadcastReceiver详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • XRecyclerView实现下拉刷新、滚动到底部加载更多等功能

    XRecyclerView实现下拉刷新、滚动到底部加载更多等功能

    这篇文章主要为大家详细介绍了XRecyclerView实现下拉刷新、滚动到底部加载更多等功能,以及添加header功能的RecyclerView,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • Android中invalidate()和postInvalidate() 的区别及使用方法

    Android中invalidate()和postInvalidate() 的区别及使用方法

    Android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用。本文给大家介绍Android中invalidate()和postInvalidate() 的区别及使用方法,感兴趣的朋友一起学习吧
    2016-05-05
  • Android中Textview和图片同行显示(文字超出用省略号,图片自动靠右边)

    Android中Textview和图片同行显示(文字超出用省略号,图片自动靠右边)

    Android中Textview和图片同行显示,文字超出用省略号显示,图片自动靠右边。看到这个问题本来认为是一个很正常的需求,看起来很简单,但是做起来却遇到了很蛋疼的问题,怎么搞的都不行,堵了很长时间,下面说一下解决的方案,希望遇到这样问题的朋友可以使用。
    2016-12-12

最新评论