zoukankan      html  css  js  c++  java
  • Android随笔

    1.src属性和background属性的区别:

    在API文档中我们发现ImageView有两个可以设置图片的属性,分别是:src和background

    常识:

    ①background通常指的都是背景,而src指的是内容!!

    ②当使用src填入图片时,是按照图片大小直接填充,并不会进行拉伸

    而使用background填入图片,则是会根据ImageView给定的宽度来进行拉伸

    1)写代码验证区别:

    写个简单的布局测试下:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        xmlns:tools="http://schemas.android.com/tools"  
        android:id="@+id/LinearLayout1"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:orientation="vertical"  
        tools:context="com.jay.example.imageviewdemo.MainActivity" >  
      
        <ImageView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:background="@drawable/pen" />  
      
        <ImageView  
            android:layout_width="200dp"  
            android:layout_height="wrap_content"  
            android:background="@drawable/pen" />  
      
        <ImageView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:src="@drawable/pen" />  
      
        <ImageView  
            android:layout_width="200dp"  
            android:layout_height="wrap_content"  
            android:src="@drawable/pen" />  
      
    </LinearLayout>

    效果图如下:

    结果分析:

    宽高都是wrap_content那就一样,是原图大小,但是,当我们固定了宽或者高的话, 差别就显而易见了,blackground完全填充了整个ImageView,而src依旧是那么大, 而且他居中了哦,这就涉及到了ImageView的另一个属性scaleType了! 另外还有一点,这里我们说了只设置width或者height哦!加入我们同时设置了 width和height的话,blackground依旧填充,但是,src的大小可能发生改变哦! 比如,我们测试下下面这段代码:

    <ImageView  
            android:layout_width="100dp"  
            android:layout_height="50dp"  
            android:src="@drawable/pen" />

    运行效果图:

  • 相关阅读:
    MYSQL router 自动均衡负载
    mysql router 自动failover测试
    Oracle数据库安装时 environment variable path 大于 1023
    windows删除多余启动引导项
    开机显示 invalid partition table
    有关软件的商业模式与软件代码的加密
    .Net 开源控件 NPlot使用小结
    41.关于Intellij IDEA菜单项中Compile、Make和Build的区别
    Maven:mirror和repository 区别
    28. Spring Boot配置方式
  • 原文地址:https://www.cnblogs.com/wrx166/p/14909535.html
Copyright © 2011-2022 走看看