android XML总结

本文介绍 Android 开发中 ImageView 的自适应属性调整及 shape.xml 文件配置技巧,包括保持图片宽高比、图片填充方式及自定义视图样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、shape .xml 

  1. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  2.         android:shape="rectangle">  
  3.         <gradient android:startColor="#c0000000" android:endColor="#c0000000"  
  4.                 android:angle="90" /><!--背景颜色渐变 -->  
  5.         <stroke android:dashWidth="2dp" android:dashGap="2dp"  
  6.                 android:width="2dp" android:color="#FF00ff00"></stroke>  
  7.         <!--描边 -->  
  8.         <corners android:bottomRightRadius="5dp"  
  9.                 android:bottomLeftRadius="5dp" android:topLeftRadius="5dp"  
  10.                 android:topRightRadius="5dp" /><!--设置圆角-->  
  11. </shape>  

gradient  产生颜色渐变  Android:angle 从哪个角度开始变 貌似只有90的整数倍可以 

android:shape="rectangle" 默认的也是长方形 

<solid android:color="#ff4100ff"/>实心的 填充里面

<stroke 描边 采用那样的方式将外形轮廓线画出来   android:dashWidth="5dp" 小横线宽度  android:dashGap="5dp"  间隔宽度实现效果如下 - - - - - -   

2、Android ImageView图片自适应

网络上下载下来的图片自适应: android:adjustViewBounds="true"(其详细解释在下面)
  1. <ImageView  
  2.     android:id="@+id/dynamic_item_image"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:layout_gravity="top"  
  6.     android:layout_marginTop="5dip"  
  7.     android:adjustViewBounds="true"  
  8.     android:background="@drawable/imageview_background" />  
另外,android:background="@drawable/imageview_background"是给图片加了一个边框,其中
imageview_background.xml:

  1. <?xml  
  2. version="1.0" encoding="utf-8"?>  
  3. <shape  xmlns:android="http://schemas.android.com/apk/res/android">  
  4. <solid android:color="@color/white"/>  
  5. <stroke android:width="2.0dip"  
  6. android:color="#99D9D9D9" /> <corners  
  7. android:radius="2.0dip" /> <padding  
  8. android:left="5.0dip" android:top="5.0dip" android:right="5.0dip"  
  9. android:bottom="5.0dip" />  
  10. </shape>  
ImageView属性说明:

1、类概述

    显示任意图像,例如图标。ImageView类可以加载各种来源的图片(如资源或图片库),需要计算图像的尺寸,比便它可以在其他布局中使用,并提供例如缩放和着色(渲染)各种显示选项。

2、XML属性

属性名称

描述

android:adjustViewBounds

是否保持宽高比。需要与maxWidthMaxHeight一起使用,否则单独使用没有效果。

android:cropToPadding

是否截取指定区域用空白代替。单独设置无效果,需要与scrollY一起使用,效果如下,实现代码见代码部分:

 

 

android:maxHeight

设置View的最大高度,单独使用无效,需要与setAdjustViewBounds一起使用。如果想设置图片固定大小,又想保持图片宽高比,需要如下设置:

1) 设置setAdjustViewBoundstrue

2) 设置maxWidthMaxHeight

3) 设置设置layout_widthlayout_heightwrap_content

android:maxWidth

设置View的最大宽度。同上。

android:scaleType

设置图片的填充方式。

matrix

0

用矩阵来绘图

 

fitXY

1

拉伸图片(不按比例)以填充View的宽高

 

layout_

height

:30px


layout_

width

:120px

fitStart

2

按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的左边

 

fitCenter

3

按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的中间

 

fitEnd

4

按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的右边

 

center

5

按原图大小显示图片,但图片宽高大于View的宽高时,截图图片中间部分显示

 

 

layout_

height

:60px


layout_

width

:80px


padding

:10px

 

centerCrop

6

按比例放大原图直至等于某边View的宽高显示。

 

centerInside

7

当原图宽高或等于View的宽高时,按原图大小居中显示;反之将原图缩放至View的宽高居中显示。

 

android:src

设置Viewdrawable(如图片,也可以是颜色,但是需要指定View的大小)

android:tint

将图片渲染成指定的颜色。见下图:

左边为原图,右边为设置后的效果,见后面代码。

3、相关布局总结

XML代码:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <RelativeLayout  
  8.         android:id="@+id/title"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentTop="true"  
  12.         android:background="#00ffff"  
  13.         android:layout_gravity="top" >  
  14.   
  15.         <Button  
  16.             android:id="@+id/button_back"  
  17.             android:layout_width="wrap_content"  
  18.             android:layout_height="wrap_content"  
  19.             android:layout_alignParentLeft="true"  
  20.             android:layout_centerVertical="true"  
  21.             android:text="返回" />  
  22.   
  23.         <TextView  
  24.             android:id="@+id/text_title"  
  25.             android:layout_width="match_parent"  
  26.             android:layout_height="wrap_content"  
  27.             android:layout_gravity="center_vertical"  
  28.             android:layout_toLeftOf="@+id/button_mark"  
  29.             android:layout_toRightOf="@id/button_back"  
  30.             android:background="#3300ff00"  
  31.             android:singleLine="false"  
  32.             android:text="当蜘蛛网无情地查封了我的炉台,当灰烬的余烟叹息着贫困的悲哀,我依然固执地铺平失望的灰烬,用美丽的雪花写下:相信未来  
  33.                 ;当我的紫葡萄化为深秋的露水,当我的鲜花依偎在别人的情怀,我依然固执地用凝霜的枯藤,在凄凉的大地上写下:相信未来  
  34.                 ;我要用手指那涌向天边的排浪,我要用手掌那托住太阳的大海,摇曳着曙光那枝温暖漂亮的笔杆,用孩子的笔体写下:相信未来" />  
  35.         <Button  
  36.             android:id="@+id/button_mark"  
  37.             android:layout_width="wrap_content"  
  38.             android:layout_height="wrap_content"  
  39.             android:layout_alignParentRight="true"  
  40.             android:layout_centerVertical="true"  
  41.             android:paddingLeft="5.0dip"  
  42.             android:paddingRight="5.0dip"  
  43.             android:text="添加书签" />  
  44.     </RelativeLayout>  
  45.   
  46. </RelativeLayout> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值