LayoutWeight属性总结
来源:互联网 发布:淘宝设计工资一般多少 编辑:程序博客网 时间:2024/05/22 06:13
关于这个属性的理解,在网上也找了好多相关的教程,总没有找到一个让我容易理解的教程,最近看了Marschen的关于这个发生的视频讲解后,感觉非常的通俗易懂,特此在这里写一个总结,以帮助更多的人能更容易的理解这个属性。
简单的理解,就是用来分配某控件点剩余空间的比例,下面通过代码来看一下效果:
---MainActivity.java---代码如下:
------------------------------------------------------分割线------------------------------------------------------------
package com.example.layoutweight;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends ActionBarActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}
------------------------------------------------------分割线------------------------------------------------------------
---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="horizontal" tools:context="com.example.layoutweight.MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0000"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#f9f900" android:text="TextView1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#0000ff" android:text="TextView2"/> </LinearLayout> </LinearLayout>
------------------------------------------------------分割线------------------------------------------------------------
上面的代码在没有加layout_weight时,显示结果如下图:
图1
当我们在TextViw1和TextViw2中加上layout_weitht=1时,显示结果如下:
图2
对比图1的结果,我们发现,两个TextView占满了一行,而且各占一行中的一半,是不是layout_weight=1就是把一半分割成两半呢,其实不是的。现在我们把TextView1的显示内容改成Text,看看结果:
图3
由结果我们可以看到,Text占的大小并没有TextView大,所以两个控制设置layout_weitht=1并不是平分一整行的,而且把剩余的空间分割成两半,每个控件各占一半。
那么如何让两个控件各占一半呢?其实很简单,我们把两个控件的layout_width属性设置成0dp即可,下面我们来看一下结果:
图4
这样就可以让每个控件各占一行的一半了。
最后我们把第一个控件的layout_weitht设置成2,第二个控件的layout_weitht设置成1,其他属性不变,main_acitivity.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="horizontal" tools:context="com.example.layoutweight.MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0000"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:background="#f9f900" android:layout_weight="2" android:text="Text" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:background="#0000ff" android:layout_weight="1" android:text="TextView2"/> </LinearLayout> </LinearLayout>
------------------------------------------------------分割线------------------------------------------------------------
运行结果如下:
图5
其他值大家可以尝试去更改,看看效果。
- LayoutWeight属性总结
- 属性总结
- CSS属性规则总结
- Ext 常用属性总结
- Android 属性总结
- DatagridView 属性,功能 总结
- Android EditText属性总结
- UIScrollView的属性总结
- MFC 属性页 总结
- 变量属性的总结
- MACHART常用属性总结
- UIScrollView的属性总结
- CSS positon 属性总结
- UIScrollView的属性总结
- UIScrollView的属性总结
- Linux线程属性总结
- 【专题属性】VOJ_DP总结
- UIScrollView的属性总结
- ARM体系结构(二)
- Sofia-SIP辅助文档二三 - Sofia SIP用户代理库 - "stun" - STUN客户端和服务端模块
- 内网域渗透
- 2016新趋势——把公司做小,把客户做大
- 字符串转数组 C++
- LayoutWeight属性总结
- Sofia-SIP辅助文档二四 - Sofia SIP用户代理库 - "features"特性模块
- CALayer设置图片等的一些属性
- JavaBean入门 - 概念和简单例子
- 最短回文串 [Codevs 3962]
- Zookeeper集群的搭建
- jdbc使用 PreparedStatement 接口实现添加数据操作
- LOG边缘检测--Marr-Hildreth边缘检测算法
- 《Nodejs开发加密货币》之十八:地址