【layout_weight权重】Android 对Layout_weight属性完全解析以及使用ListView来实现表格

本文涉及的产品
云解析DNS-重点域名监控,免费拨测 20万次(价值200元)
简介: 转载请注明出处:http://blog.csdn.net/xiaanming/article/details/13630837 今天主要说的是对Layout_weight属性的完全解析,以及利用Layout_...

转载请注明出处:http://blog.csdn.net/xiaanming/article/details/13630837

今天主要说的是对Layout_weight属性的完全解析,以及利用Layout_weight这个属性使用ListView来实现表格的效果,我们都知道Android里面专门有一个TableLayout来实现表格的,说实话,我平常开发中用TableLayout还是比较少的,几乎没有用到,我们完全可以用LinearLayout和RelativeLayout来代替TableLayout的使用,自己开发中主要使用LinearLayout,RelativeLayout这两种布局,不过刚开始我还是偏爱于RelativeLayout,因为在RelativeLayout里面我们可以直接拖拽控件来布局,比较方便,现在对这两种布局偏爱各半吧,LinearLayout里面有一个属性android:layout_weight比较重要,我们在开发中常常使用它来调节界面效果,也行很多人还不了解这个属性的使用,不过没关系,我首先先带大家理解android:layout_weight属性然后在利用它来实现一个表格效果

android:layout_weight是指LinearLayout先给里面的控件分配完大小之后剩余空间的权重,也许你暂时还是摸不到头脑,不过没有关系,下面我通过例子来解释layout_weight到底是什么意思,先看下面的布局文件,一个LinearLayout,里面3个文本框

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:background="#0045f5"  
  11.         android:gravity="center"  
  12.         android:text="1" />  
  13.   
  14.     <TextView  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:background="#00ff47"  
  18.         android:gravity="center"  
  19.         android:text="2"   
  20.         android:layout_weight="1"/>  
  21.   
  22.     <TextView  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:background="#ff5600"  
  26.         android:gravity="center"  
  27.         android:layout_weight="1"  
  28.         android:text="3" />  
  29.   
  30. </LinearLayout>  


为什么效果是这个样子呢,首先3个文本框的宽度都是“wrap_content”,根据视图内部内容自动扩展,LinearLayout就先给3个TextView分配空间适当的空间大小,假设为每个TextView分配10dip的宽度,屏幕的宽度为480dip, 那么LinearLayout的剩余空间就是 480 - 3*10 = 450dip,由于第一个TextView没有设置layout_weight,所以它的宽度就是10dip,而后面两个TextView设置layout_weight都是1,所以后面两个TextView就平均分配LinearLayout的剩余空间,即为 450 / 2  = 225dip,所以后面两个TextView的宽度为10 + 225 = 235dip


如果我们实际开发中,你设置里面控件的宽度为”wrap_content“,然后想让里面的控件按比例占用大小,那么你就大错特错了,为什么呢?我们看看下面的代码

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:background="#0045f5"  
  11.         android:gravity="center"  
  12.         android:text="1" />  
  13.   
  14.     <TextView  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:background="#00ff47"  
  18.         android:gravity="center"  
  19.         android:text="2222222222222222222"   
  20.         android:layout_weight="1"/>  
  21.   
  22.     <TextView  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:background="#ff5600"  
  26.         android:gravity="center"  
  27.         android:layout_weight="1"  
  28.         android:text="3" />  
  29. </LinearLayout>  

你本来想让后面两个TextView平均分配剩余控件,可是下面的效果却并不是你想要的,如下图



其实因为3个TextView的宽度都是”wrap_content“,LinearLayout会先按照TextView里面的内容分配好大小,由于第2个TextView内容很多,所以LinearLayout为其分配更多的空间,使得剩余空间变小了,原理和上面的一样,那么我们在实际开发中要怎么设置按比例分配呢。知道原理其实就很简单,比如我们想要3个TextView按照1:2:3的效果

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="0dip"  
  9.         android:layout_height="wrap_content"  
  10.         android:background="#0045f5"  
  11.         android:gravity="center"  
  12.         android:layout_weight="1"  
  13.         android:text="1" />  
  14.   
  15.     <TextView  
  16.         android:layout_width="0dip"  
  17.         android:layout_height="wrap_content"  
  18.         android:background="#00ff47"  
  19.         android:gravity="center"  
  20.         android:text="2222222222222222222"   
  21.         android:layout_weight="2"/>  
  22.   
  23.     <TextView  
  24.         android:layout_width="0dip"  
  25.         android:layout_height="wrap_content"  
  26.         android:background="#ff5600"  
  27.         android:gravity="center"  
  28.         android:layout_weight="3"  
  29.         android:text="3" />  
  30. </LinearLayout>  


我们只需要将3个TextView的宽度设置为0dip,首先LinearLayout为3个TextView分配0dip的宽度,剩余空间就是 480 - 3 * 0 = 480dip,然后剩余空间在按照权重分配,所以我们看到的效果就是1:2:3


通过上面的讲解,也许你会得出一个结论,权重越大,LinearLayout为其分配的空间就越大,我只能说这个结论下有点早了,我们继续看布局

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:background="#0045f5"  
  11.         android:gravity="center"  
  12.         android:layout_weight="1"  
  13.         android:text="1" />  
  14.   
  15.     <TextView  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:background="#00ff47"  
  19.         android:gravity="center"  
  20.         android:text="2"   
  21.         android:layout_weight="2"/>  
  22.   
  23.     <TextView  
  24.         android:layout_width="fill_parent"  
  25.         android:layout_height="wrap_content"  
  26.         android:background="#ff5600"  
  27.         android:gravity="center"  
  28.         android:layout_weight="2"  
  29.         android:text="3" />  
  30. </LinearLayout>  


也许你会很纳闷,怎么不是你想要的1:2:2的效果,我来为你解决疑惑吧,原理跟上面的还是一样的,因为我们这里为每个TextView设置的宽度为”fill_parent",即为充满整个LinearLayout,假如屏幕依然为480dip, 首先LinearLayout为3个TextView分配的宽度为480dip,屏幕剩余宽度为 480 - 3* 480 = -960dip,然后3个TextView按照权重分配剩余空间,第一个TextView分配宽度为 480 + (-960) * (1/5) = 288dip,后面两个TextView就为480 + (-960) * (2/5) = 96dip,比例为3:1:1


通过上面的例子和分析,你是不是对Layout_weight属性理解很透彻了呢,如果我们想要按照权重比例来分配LinearLayout,我们需要将其宽度设置为0dip,如果我们将其宽度设置为“fill_parent"的时候,其控件所占的比例不是权重的比例,我们需要自行计算比例


接下来我们就通过Layout_weight用ListView来实现表格,我们先看Activity的布局

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:orientation="vertical"  
  4.     android:layout_margin="10dip"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent" >  
  7.       
  8.     <include   
  9.         layout="@layout/list_item"  
  10.         android:id="@+id/table_title"/>  
  11.   
  12.     <ListView  
  13.         android:id="@+id/list"  
  14.         android:divider="#f9b68b"  
  15.         android:dividerHeight="1.0dip"  
  16.         android:scrollbars="none"  
  17.         android:background="@drawable/listview_bg"  
  18.         android:cacheColorHint="@android:color/transparent"  
  19.         android:fadingEdge="none"  
  20.         android:layout_width="fill_parent"  
  21.         android:layout_height="wrap_content" >  
  22.     </ListView>  
  23.   
  24. </LinearLayout>  
一个线性布局,然后就是表格的title布局,下面就是一个ListView了,很简单的布局,接下来就是ListView每个item的布局

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="wrap_content" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/text_name"  
  9.         android:layout_width="0dip"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_weight="2"  
  12.         android:gravity="center"  
  13.         android:paddingBottom="10dip"  
  14.         android:paddingTop="10dip"  
  15.         android:text="姓名" />  
  16.       
  17.     <View   
  18.         android:layout_width="1.5dip"  
  19.         android:layout_height="fill_parent"  
  20.         android:background="#f9b68b"/>  
  21.   
  22.     <TextView  
  23.         android:id="@+id/text_sex"  
  24.         android:layout_width="0dip"  
  25.         android:layout_height="wrap_content"  
  26.         android:layout_weight="1"  
  27.         android:paddingBottom="10dip"  
  28.         android:paddingTop="10dip"  
  29.         android:gravity="center"  
  30.         android:text="性别" />  
  31.       
  32.      <View   
  33.         android:layout_width="1.5dip"  
  34.         android:layout_height="fill_parent"  
  35.         android:background="#f9b68b"/>  
  36.   
  37.     <TextView  
  38.         android:id="@+id/text_age"  
  39.         android:layout_width="0dip"  
  40.         android:layout_height="wrap_content"  
  41.         android:layout_weight="1"  
  42.         android:paddingBottom="10dip"  
  43.         android:paddingTop="10dip"  
  44.         android:gravity="center"  
  45.         android:text="年龄" />  
  46.   
  47. </LinearLayout>  
3个TextView的宽度都是0dip,那两个View是中间的分割线,3个TextView的权重比值是2:1:1 ,这样子3个TextView不会因为里面内容的长度而变形

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.listviewtable;  
  2.   
  3. public class Person {  
  4.     private String name;  
  5.     private String sex;  
  6.     private int age;  
  7.       
  8.     public Person() {  
  9.         super();  
  10.     }  
  11.       
  12.     public Person(String name, String sex, int age) {  
  13.         super();  
  14.         this.name = name;  
  15.         this.sex = sex;  
  16.         this.age = age;  
  17.     }  
  18.     public String getName() {  
  19.         return name;  
  20.     }  
  21.     public void setName(String name) {  
  22.         this.name = name;  
  23.     }  
  24.     public String getSex() {  
  25.         return sex;  
  26.     }  
  27.     public void setSex(String sex) {  
  28.         this.sex = sex;  
  29.     }  
  30.     public int getAge() {  
  31.         return age;  
  32.     }  
  33.     public void setAge(int age) {  
  34.         this.age = age;  
  35.     }  
  36.       
  37.       
  38. }  
用来存放ListView每个item数据的实体类

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.listviewtable;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.content.Context;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.BaseAdapter;  
  10. import android.widget.TextView;  
  11.   
  12. public class TableAdapter extends BaseAdapter {  
  13.     private List<Person> list;  
  14.     private LayoutInflater inflater;  
  15.       
  16.     public TableAdapter(Context context, List<Person> list){  
  17.         this.list = list;  
  18.         inflater = LayoutInflater.from(context);  
  19.     }  
  20.   
  21.     @Override  
  22.     public int getCount() {  
  23.         return list.size();  
  24.     }  
  25.   
  26.     @Override  
  27.     public Object getItem(int position) {  
  28.         return list.get(position);  
  29.     }  
  30.   
  31.     @Override  
  32.     public long getItemId(int position) {  
  33.         return position;  
  34.     }  
  35.   
  36.     @Override  
  37.     public View getView(int position, View convertView, ViewGroup parent) {  
  38.         Person person = (Person) this.getItem(position);  
  39.         ViewHolder viewHolder;  
  40.         if(convertView == null){  
  41.             viewHolder = new ViewHolder();  
  42.             convertView = inflater.inflate(R.layout.list_item, null);  
  43.             viewHolder.mTextName = (TextView) convertView.findViewById(R.id.text_name);  
  44.             viewHolder.mTextSex = (TextView) convertView.findViewById(R.id.text_sex);  
  45.             viewHolder.mTextAge = (TextView) convertView.findViewById(R.id.text_age);  
  46.             convertView.setTag(viewHolder);  
  47.         }else{  
  48.             viewHolder = (ViewHolder) convertView.getTag();  
  49.         }  
  50.           
  51.         viewHolder.mTextName.setText(person.getName());  
  52.         viewHolder.mTextSex.setText(person.getSex());  
  53.         viewHolder.mTextAge.setText(person.getAge() + "岁");  
  54.           
  55.           
  56.         return convertView;  
  57.     }  
  58.       
  59.     public static class ViewHolder{  
  60.         public TextView mTextName;  
  61.         public TextView mTextSex;  
  62.         public TextView mTextAge;  
  63.           
  64.     }  
  65.   
  66. }  
ListView的适配器类,代码很简单,我也没有注释也不去讲解,相信大家都看得懂这些代码,这就是一个基本的自己定义的适配器类,最后就是Activity界面代码

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.listviewtable;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.graphics.Color;  
  8. import android.os.Bundle;  
  9. import android.view.ViewGroup;  
  10. import android.widget.ListView;  
  11.   
  12. public class ListTableActivity extends Activity {  
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.           
  19.         //设置表格标题的背景颜色  
  20.         ViewGroup tableTitle = (ViewGroup) findViewById(R.id.table_title);  
  21.         tableTitle.setBackgroundColor(Color.rgb(25510010));  
  22.           
  23.         List<Person> list = new ArrayList<Person>();  
  24.         list.add(new Person("刘德华""男"50));  
  25.         list.add(new Person("刘德华""男"50));  
  26.         list.add(new Person("刘德华""男"50));  
  27.         list.add(new Person("刘德华""男"50));  
  28.         list.add(new Person("刘德华""男"50));  
  29.         list.add(new Person("刘德华""男"50));  
  30.         list.add(new Person("刘德华""男"50));  
  31.         list.add(new Person("刘德华""男"50));  
  32.         list.add(new Person("刘德华""男"50));  
  33.         list.add(new Person("刘德华""男"50));  
  34.           
  35.         ListView tableListView = (ListView) findViewById(R.id.list);  
  36.         TableAdapter adapter = new TableAdapter(this, list);  
  37.         tableListView.setAdapter(adapter);  
  38.     }  
  39.   
  40.   
  41. }  
运行程序效果如下:




通过layout_weight这个属性,我们就轻松实现了表格的功能,通过本文章相信大家对这个属性有了深刻的理解,大家有什么疑问可以在下面留言,我会为大家解答的
项目源码,点击下载

相关文章
|
2月前
|
数据采集 监控 API
告别手动埋点!Android 无侵入式数据采集方案深度解析
传统的Android应用监控方案需要开发者在代码中手动添加埋点,不仅侵入性强、工作量大,还难以维护。本文深入探讨了基于字节码插桩技术的无侵入式数据采集方案,通过Gradle插件 + AGP API + ASM的技术组合,实现对应用性能、用户行为、网络请求等全方位监控,真正做到零侵入、易集成、高稳定。
502 40
|
7月前
|
数据库 Android开发
Android使用EditText+Listview实现搜索效果(使用room模糊查询)
本文介绍如何在Android中使用EditText与ListView实现搜索功能,并结合Room数据库完成模糊查询。主要内容包括:Room的模糊查询语句(使用`||`代替`+`号)、布局美化(如去除ListView分割线和EditText下划线)、EditText回车事件监听,以及查询逻辑代码示例。此外,还提供了相关扩展文章链接,帮助读者深入了解ListView优化、动态搜索及Room基础操作。
508 65
|
7月前
|
Android开发 开发者
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
本文详细介绍了如何通过自定义 `attrs.xml` 文件实现 Android 自定义 View 的属性配置。以一个包含 TextView 和 ImageView 的 DemoView 为例,讲解了如何使用自定义属性动态改变文字内容和控制图片显示隐藏。同时,通过设置布尔值和点击事件,实现了图片状态的切换功能。代码中展示了如何在构造函数中解析自定义属性,并通过方法 `setSetting0n` 和 `setbackeguang` 实现功能逻辑的优化与封装。此示例帮助开发者更好地理解自定义 View 的开发流程与 attrs.xml 的实际应用。
199 2
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
|
Java 开发工具 Android开发
Android与iOS开发环境搭建全解析####
本文深入探讨了Android与iOS两大移动操作系统的开发环境搭建流程,旨在为初学者及有一定基础的开发者提供详尽指南。我们将从开发工具的选择、环境配置到第一个简单应用的创建,一步步引导读者步入移动应用开发的殿堂。无论你是Android Studio的新手还是Xcode的探索者,本文都将为你扫清开发道路上的障碍,助你快速上手并享受跨平台移动开发的乐趣。 ####
|
6月前
|
安全 Java Android开发
为什么大厂要求安卓开发者掌握Kotlin和Jetpack?深度解析现代Android开发生态优雅草卓伊凡
为什么大厂要求安卓开发者掌握Kotlin和Jetpack?深度解析现代Android开发生态优雅草卓伊凡
289 0
为什么大厂要求安卓开发者掌握Kotlin和Jetpack?深度解析现代Android开发生态优雅草卓伊凡
|
9月前
|
数据采集 JSON 数据可视化
JSON数据解析实战:从嵌套结构到结构化表格
在信息爆炸的时代,从杂乱数据中提取精准知识图谱是数据侦探的挑战。本文以Google Scholar为例,解析嵌套JSON数据,提取文献信息并转换为结构化表格,通过Graphviz制作技术关系图谱,揭示文献间的隐秘联系。代码涵盖代理IP、请求头设置、JSON解析及可视化,提供完整实战案例。
569 4
JSON数据解析实战:从嵌套结构到结构化表格
|
9月前
|
数据采集 前端开发 JavaScript
金融数据分析:解析JavaScript渲染的隐藏表格
本文详解了如何使用Python与Selenium结合代理IP技术,从金融网站(如东方财富网)抓取由JavaScript渲染的隐藏表格数据。内容涵盖环境搭建、代理配置、模拟用户行为、数据解析与分析等关键步骤。通过设置Cookie和User-Agent,突破反爬机制;借助Selenium等待页面渲染,精准定位动态数据。同时,提供了常见错误解决方案及延伸练习,帮助读者掌握金融数据采集的核心技能,为投资决策提供支持。注意规避动态加载、代理验证及元素定位等潜在陷阱,确保数据抓取高效稳定。
285 17
|
9月前
|
XML JavaScript Android开发
【Android】网络技术知识总结之WebView,HttpURLConnection,OKHttp,XML的pull解析方式
本文总结了Android中几种常用的网络技术,包括WebView、HttpURLConnection、OKHttp和XML的Pull解析方式。每种技术都有其独特的特点和适用场景。理解并熟练运用这些技术,可以帮助开发者构建高效、可靠的网络应用程序。通过示例代码和详细解释,本文为开发者提供了实用的参考和指导。
323 15
|
9月前
|
监控 Shell Linux
Android调试终极指南:ADB安装+多设备连接+ANR日志抓取全流程解析,覆盖环境变量配置/多设备调试/ANR日志分析全流程,附Win/Mac/Linux三平台解决方案
ADB(Android Debug Bridge)是安卓开发中的重要工具,用于连接电脑与安卓设备,实现文件传输、应用管理、日志抓取等功能。本文介绍了 ADB 的基本概念、安装配置及常用命令。包括:1) 基本命令如 `adb version` 和 `adb devices`;2) 权限操作如 `adb root` 和 `adb shell`;3) APK 操作如安装、卸载应用;4) 文件传输如 `adb push` 和 `adb pull`;5) 日志记录如 `adb logcat`;6) 系统信息获取如屏幕截图和录屏。通过这些功能,用户可高效调试和管理安卓设备。
|
存储 Linux API
深入探索Android系统架构:从内核到应用层的全面解析
本文旨在为读者提供一份详尽的Android系统架构分析,从底层的Linux内核到顶层的应用程序框架。我们将探讨Android系统的模块化设计、各层之间的交互机制以及它们如何共同协作以支持丰富多样的应用生态。通过本篇文章,开发者和爱好者可以更深入理解Android平台的工作原理,从而优化开发流程和提升应用性能。

热门文章

最新文章

推荐镜像

更多
  • DNS