• [转]用android LinearLayout和RelativeLayout实现精确布局


    先明确几个概念的区别: 
    padding margin都是边距的含义,关键问题得明白是什么相对什么的边距. 
    padding是控件的内容相对控件的边缘的边距. 
    margin是控件边缘相对父控件的边距. 


     

    android:gravity 属性是对该view 内容的限定.比如一个button 上面的text. 你可以设置该text 在view的靠左,靠右等位置.该属性就干了这个. 
    android:layout_gravity是用来设置该view中的子view相对于父view的位置.比如一个button 在linearlayout里,你想把该button放在靠左,靠右等位置就可以在linearlayout中通过该属性设置. 

    下面看布局文件及效果图 

    Java代码  收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="horizontal" android:layout_width="fill_parent"  
    4.     android:layout_height="wrap_content" android:gravity="center_vertical">  
    5.     <ImageView android:id="@+id/ivLogo" android:layout_width="50dp"  
    6.         android:layout_height="50dp" android:src="@drawable/icon"  
    7.         android:paddingLeft="5dp" />  
    8.     <RelativeLayout android:id="@+id/rl_name"  
    9.         android:layout_width="wrap_content"  
    10.         android:layout_height="wrap_content" android:gravity="right"  
    11.         android:padding="10dp">  
    12.         <TextView android:id="@+id/tvApplicationName"  
    13.             android:layout_width="wrap_content" android:layout_height="wrap_content"  
    14.             android:textSize="16dp" />  
    15.     </RelativeLayout>  
    16.     <RelativeLayout android:id="@+id/rl_score"  
    17.         android:layout_width="fill_parent"  
    18.         android:layout_height="wrap_content" android:gravity="right"  
    19.         android:padding="10dp">  
    20.         <TextView android:id="@+id/tvRating" android:layout_width="wrap_content"  
    21.             android:layout_height="wrap_content" android:text="5.0" />  
    22.         <RatingBar android:id="@+id/ratingbar" android:layout_width="wrap_content"  
    23.             android:layout_height="wrap_content" android:numStars="5"  
    24.             style="?android:attr/ratingBarStyleSmall" android:layout_below="@id/tvRating" />  
    25.     </RelativeLayout>  
    26. </LinearLayout>  



     

    上面的布局文件是一个ListView中的list_item布局,在一个ListView中显示所有的APK资源,每个资源项显示图标,名称及评分。在listItem的最外层LinearLayout中加android:gravity="center_vertical",设定内容垂直居中显示。在id为rl_score的RelativeLayout中设定android:layout_width="fill_parent"来填充剩余空间;android:gravity="right"设定内容相对于rl_score右对齐;android:padding="10dp"设定RelativeLayout中的内容相对RelativeLayout的边缘的边距为10dp。 
    这个布局虽然简单,但却是经常用到的。 
    引用请注明出处:http://zhangkun716717-126-com.iteye.com/

  • 相关阅读:
    dotnet 6 使用 HttpWebRequest 进行 POST 文件将占用大量内存
    递增运算符重载
    左移运算符重载
    流程与标准
    Erp系统常用递归,查类目树,查上级,查下级
    判断一个点是否在指定的直角三角形内
    [LeetCode]2281. Sum of Total Strength of Wizards 计算公式推导详解(JavaScript版)
    Airtest+Poco常见Exception报错
    nodejs连接mysql数据库,报错Client does not support authentication protocol requested by server的解决方法
    Poco API精讲之元素树冻结freeze()
  • 原文地址:https://www.cnblogs.com/xunbu7/p/5003353.html
Copyright © 2020-2023  润新知