zoukankan      html  css  js  c++  java
  • Android开发技巧一--weight属性实现视图的居中(半)显示

    面试时,一位面试官问到:“如果我想讲按钮居中显示,并且占据其父视图宽度的一半,应该怎么做到呢?”即实现这种效果:

    我们使用weightSum属性和layout_weight属性实现这一要求:

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent"  
    5.     android:orientation="horizontal"  
    6.     android:gravity="center"  
    7.     android:weightSum="1" >  
    8.       
    9.   
    10.     <Button  
    11.         android:id="@+id/btn"  
    12.         android:layout_width="0dp"  
    13.         android:layout_height="wrap_content"  
    14.         android:text="翻  译"  
    15.         android:layout_weight="0.5" />  
    16.      
    17.           
    18.   
    19. </LinearLayout>  

    其中有三个重要的设定

    1.设定LinearLayout的android:weightSum属性值为1,表示其内部所有子视图的weight比例总和为1;

    2.而LinearLayout中只有唯一的一个子视图Button,因此Button的android:layout_weight="0.5;

    3.要想weight属性在水平方向上起作用,那么设置layout_weight的宽度为0dp。

  • 相关阅读:
    Scheduled定时任务的触发规则
    linux设置系统时间为当前网络时间
    idea控制台打印日志出现乱码
    Nginx配置高可用的集群
    Nginx实现负载均衡
    BigDecimal比较大小
    Java调用第三方系统接口获取数据
    sql中主要关键字的执行顺序
    ASP.NET.Core --Jenkins+Docker
    ASP.NET.Core --Swagger+Route
  • 原文地址:https://www.cnblogs.com/likeju/p/4838008.html
Copyright © 2011-2022 走看看