1、shape .xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="#c0000000" android:endColor="#c0000000" android:angle="90" /><!--背景颜色渐变 --> <stroke android:dashWidth="2dp" android:dashGap="2dp" android:width="2dp" android:color="#FF00ff00"></stroke> <!--描边 --> <corners android:bottomRightRadius="5dp" android:bottomLeftRadius="5dp" android:topLeftRadius="5dp" android:topRightRadius="5dp" /><!--设置圆角--> </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"(其详细解释在下面)
<ImageView android:id="@+id/dynamic_item_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" android:layout_marginTop="5dip" android:adjustViewBounds="true" android:background="@drawable/imageview_background" />
另外,android:background="@drawable/imageview_background"是给图片加了一个边框,其中
imageview_background.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/white"/> <stroke android:width="2.0dip" android:color="#99D9D9D9" /> <corners android:radius="2.0dip" /> <padding android:left="5.0dip" android:top="5.0dip" android:right="5.0dip" android:bottom="5.0dip" /> </shape>
ImageView属性说明:
1、类概述
显示任意图像,例如图标。ImageView类可以加载各种来源的图片(如资源或图片库),需要计算图像的尺寸,比便它可以在其他布局中使用,并提供例如缩放和着色(渲染)各种显示选项。
2、XML属性
属性名称 |
描述 |
|||||||||||||||||||||||||||
android:adjustViewBounds |
是否保持宽高比。需要与maxWidth、MaxHeight一起使用,否则单独使用没有效果。 |
|||||||||||||||||||||||||||
android:cropToPadding |
是否截取指定区域用空白代替。单独设置无效果,需要与scrollY一起使用,效果如下,实现代码见代码部分: |
|||||||||||||||||||||||||||
android:maxHeight |
设置View的最大高度,单独使用无效,需要与setAdjustViewBounds一起使用。如果想设置图片固定大小,又想保持图片宽高比,需要如下设置: 1) 2) 3) |
|||||||||||||||||||||||||||
android:maxWidth |
设置View的最大宽度。同上。 |
|||||||||||||||||||||||||||
android:scaleType |
设置图片的填充方式。
|
|||||||||||||||||||||||||||
android:src |
设置View的drawable(如图片,也可以是颜色,但是需要指定View的大小) |
|||||||||||||||||||||||||||
android:tint |
将图片渲染成指定的颜色。见下图: 左边为原图,右边为设置后的效果,见后面代码。 |
3、相关布局总结
XML代码:
<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=".MainActivity" > <RelativeLayout android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:background="#00ffff" android:layout_gravity="top" > <Button android:id="@+id/button_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:text="返回" /> <TextView android:id="@+id/text_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_toLeftOf="@+id/button_mark" android:layout_toRightOf="@id/button_back" android:background="#3300ff00" android:singleLine="false" android:text="当蜘蛛网无情地查封了我的炉台,当灰烬的余烟叹息着贫困的悲哀,我依然固执地铺平失望的灰烬,用美丽的雪花写下:相信未来 ;当我的紫葡萄化为深秋的露水,当我的鲜花依偎在别人的情怀,我依然固执地用凝霜的枯藤,在凄凉的大地上写下:相信未来 ;我要用手指那涌向天边的排浪,我要用手掌那托住太阳的大海,摇曳着曙光那枝温暖漂亮的笔杆,用孩子的笔体写下:相信未来" /> <Button android:id="@+id/button_mark" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:paddingLeft="5.0dip" android:paddingRight="5.0dip" android:text="添加书签" /> </RelativeLayout> </RelativeLayout>