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

其他值大家可以尝试去更改,看看效果。


0 0
原创粉丝点击