Android布局之LinerLayout点滴

LinerLayout顾明思议,线性布局,指定VIEW只能按横向或者竖线进行依次排列。

android:orientation:vertical(竖向排列) ;horizontal(横向排列)


示例演示:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    

    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button1" />
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button2" />
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button3" />
            
</LinearLayout>



效果如下:

修改

android:orientation="vertical",再看下效果
</pre><img src="https://img-blog.csdn.net/20151215120612954?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /><p></p><p></p><p>有了这个属性,开始我们的UI旅程吧,写一个计算器界面,参考系统计算器</p><p></p><p></p><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dip"
        android:orientation="horizontal">
        
        <Button 
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:layout_margin="2dip"
            android:text="BackSpace" />
        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="2dip"
            android:layout_gravity="center_vertical"
            android:text="CE" />
         <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="2dip"
            android:layout_gravity="bottom"
            android:text="C" />
         
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
         
        <Button 
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="2dip"
            android:text="7" />
        
        <Button 
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="2dip"
            android:text="8" />
        
        <Button 
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="2dip"
            android:text="9" />
        
        <Button 
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="2dip"
            android:text="/" />
        
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
         
        <Button 
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="2dip"
            android:text="4" />
        
        <Button 
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="2dip"
            android:text="5" />
        
        <Button 
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="2dip"
            android:text="6" />
        
        <Button 
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="2dip"
            android:text="*" />
        
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
         
        <Button 
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="2dip"
            android:text="1" />
        
        <Button 
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="2dip"
            android:text="2" />
        
        <Button 
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="2dip"
            android:text="3" />
        
        <Button 
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="2dip"
            android:text="-" />
        
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
         
        <Button 
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="2dip"
            android:text="0" />
        
        <Button 
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="2dip"
            android:text="+/-" />
        
        <Button 
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="2dip"
            android:text="." />
        
        <Button 
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="2dip"
            android:text="+" />
        
    </LinearLayout>

</LinearLayout>

效果:

已经实现了计算器部分界面,自己补充完整即可。


不对,第一行排列为什么不整齐呢,小伙伴们也发现了

android:layout_weight="1"
android:layout_margin="2dip"
android:layout_gravity="bottom"


layout_weight属性:将各个VIEW宽度进行平均按比例分配;

layout_margin属性:与其它VIEW之间间距设置

lyaout_gravity属性:设置在父VIEW中的对其方式,重点说明,如果LinerLayout的orientation设置的为horizontal,则属性值top bottom 有效;如果设置为vertical则left right等有效。


让我们依次看下效果吧

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    

    <Button 
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="1" />
    
    <Button 
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="2" />
    
    <Button 
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="3" />
            
</LinearLayout>

button1占用50%控件,button2,3分别占用25%空间。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值