• 吴裕雄--天生自然ANDROID开发学习:2.2.2 RelativeLayout(相对布局)


    具体问题具体分析,总结就是:尽量使用RelativeLayout + LinearLayout的weight属性搭配使用

     

     

     

     

     

    实现代码:
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
        xmlns:tools="http://schemas.android.com/tools"    
        android:id="@+id/RelativeLayout1"    
        android:layout_width="match_parent"    
        android:layout_height="match_parent" >    
        
        <!-- 这个是在容器中央的 -->    
            
        <ImageView    
            android:id="@+id/img1"     
            android:layout_width="80dp"    
            android:layout_height="80dp"    
            android:layout_centerInParent="true"    
            android:src="@drawable/pic1"/>    
            
        <!-- 在中间图片的左边 -->    
        <ImageView    
            android:id="@+id/img2"     
            android:layout_width="80dp"    
            android:layout_height="80dp"    
            android:layout_toLeftOf="@id/img1"    
            android:layout_centerVertical="true"    
            android:src="@drawable/pic2"/>    
            
        <!-- 在中间图片的右边 -->    
        <ImageView    
            android:id="@+id/img3"     
            android:layout_width="80dp"    
            android:layout_height="80dp"    
            android:layout_toRightOf="@id/img1"    
            android:layout_centerVertical="true"    
            android:src="@drawable/pic3"/>    
            
        <!-- 在中间图片的上面-->    
        <ImageView    
            android:id="@+id/img4"     
            android:layout_width="80dp"    
            android:layout_height="80dp"    
            android:layout_above="@id/img1"    
            android:layout_centerHorizontal="true"    
            android:src="@drawable/pic4"/>    
            
        <!-- 在中间图片的下面 -->    
        <ImageView    
            android:id="@+id/img5"     
            android:layout_width="80dp"    
            android:layout_height="80dp"    
            android:layout_below="@id/img1"    
            android:layout_centerHorizontal="true"    
            android:src="@drawable/pic5"/>    
        
    </RelativeLayout>
    4.margin与padding的区别
    初学者对于这两个属性可能会有一点混淆,这里区分下: 首先margin代表的是偏移,比如marginleft = "5dp"表示组件离容器左边缘偏移5dp; 而padding代表的则是填充,而填充的对象针对的是组件中的元素,比如TextView中的文字 比如为TextView设置paddingleft = "5dp",则是在组件里的元素的左边填充5dp的空间! margin针对的是容器中的组件,而padding针对的是组件中的元素,要区分开来! 下面通过简单的代码演示两者的区别:
    
    比较示例代码如下:
    
    <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"    
        android:paddingLeft="@dimen/activity_horizontal_margin"    
        android:paddingRight="@dimen/activity_horizontal_margin"    
        android:paddingTop="@dimen/activity_vertical_margin"    
        tools:context=".MainActivity" >    
        
        <Button    
            android:id="@+id/btn1"     
            android:layout_height="wrap_content"    
            android:layout_width="wrap_content"    
            android:text="Button"/>    
        <Button    
            android:paddingLeft="100dp"     
            android:layout_height="wrap_content"    
            android:layout_width="wrap_content"    
            android:text="Button"    
            android:layout_toRightOf="@id/btn1"/>    
            
        <Button    
            android:id="@+id/btn2"     
            android:layout_height="wrap_content"    
            android:layout_width="wrap_content"    
            android:text="Button"    
            android:layout_alignParentBottom="true"/>    
        <Button    
            android:layout_marginLeft="100dp"     
            android:layout_height="wrap_content"    
            android:layout_width="wrap_content"    
            android:text="Button"    
            android:layout_toRightOf="@id/btn2"     
            android:layout_alignParentBottom="true"/>    
            
    </RelativeLayout> 

    5.很常用的一点:margin可以设置为负数
    平时我们设置margin的时候都习惯了是正数的, 其实是可以用负数的,下面写个简单的程序演示下吧,模拟进入软件后,弹出广告 页面的,右上角的cancle按钮的margin则是使用负数的!
    
    效果图如下:

    贴出的广告Activity的布局代码吧,当然,如果你对这个有兴趣的话可以下下demo, 因为仅仅是实现效果,所以代码会有些粗糙!
    
    <RelativeLayout 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"  
        tools:context="com.jay.example.relativelayoutdemo.MainActivity"   
        android:background="#00CCCCFF">  
      
        <ImageView  
            android:id="@+id/imgBack"  
            android:layout_width="200dp"  
            android:layout_height="200dp"  
            android:layout_centerInParent="true"  
            android:background="@drawable/myicon" />  
      
        <ImageView  
            android:id="@+id/imgCancle"  
            android:layout_width="28dp"  
            android:layout_height="28dp"  
            android:layout_alignRight="@id/imgBack"  
            android:layout_alignTop="@id/imgBack"  
            android:background="@drawable/cancel"  
            android:layout_marginTop="-15dp"  
            android:layout_marginRight="-10dp" />  
      
    </RelativeLayout>  
  • 相关阅读:
    知识搜索
    使用 getopt() 进行命令行处理
    【新提醒】夏新大v安卓4.1尝鲜最新更新版本发布(包含进步版)1124更新 大V综合交流区 360论坛
    搜狗知立方高调亮相 开启知识计算新时代
    socat: Linux / UNIX TCP Port Forwarder
    Crontab 每两周执行一次
    python 命令行解析 optionparser
    crontab jojo's blog--快乐忧伤都与你同在 BlogJava
    搜索引擎开始「实体搜索」新领域竞争,Google、百度分别发力实体搜索产品
    netcat(nc)的替代产品 Socat
  • 原文地址:https://www.cnblogs.com/tszr/p/12993513.html
Copyright © 2020-2023  润新知