LayoutWeight属性总结

本文详细解析了Android布局中layout_weight属性的作用与用法。通过具体实例展示了如何使用layout_weight来调整视图组件的大小与位置,特别是针对水平布局中的空间分配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于这个属性的理解,在网上也找了好多相关的教程,总没有找到一个让我容易理解的教程,最近看了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 {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}


@Override
public 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;
}


@Override
public 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

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值