如何给ImageView加边框?

loskill 2011-02-24 02:30:27
<ImageView
android:layout_below="@+id/LinearLayout05"
android:layout_alignLeft="@+id/LinearLayout05"
android:id="@+id/ImageView01"
android:src="#FFFF0000"
android:layout_height="80dp"
android:layout_width="60dp"
android:background="@android:drawable/btn_default"
android:paddingBottom="20dp"
android:paddingLeft="120dp"
android:paddingRight="20dp"
android:paddingTop="20dp"
android:scaleType="fitCenter">
</ImageView>
我这样设置后,整个ImageView都是红色的,背景完全被盖住了。该如何给ImageView加边框啊?
...全文
7038 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangliyewoaini 2011-03-24
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 tairikun 的回复:]

用不着那么麻烦,ImageView设置个Margin属性就可以;然后放入你想放入的View中即可。

<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@dra……
[/Quote]

这个方法,简单,实用,很好,还有一种方法也是这样的。
定义一个

<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#000000" />
<stroke android:width="3.0dip" android:color="#000000" />
<corners android:radius="2.0dip" />
<padding android:left="0.0dip" android:top="0.0dip"
android:right="0.0dip" android:bottom="0.0dip" />
</shape>

<ImageView
android:id="@+id/allimageview"
android:layout_width="114px"
android:layout_height="80px"

android:layout_margin="8dip"
android:layout_alignParentLeft="true"
android:background="@drawable/sms_type_bg"
/>
树袋熊TRK 2011-03-07
  • 打赏
  • 举报
回复
用不着那么麻烦,ImageView设置个Margin属性就可以;然后放入你想放入的View中即可。

<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/color_orange" android:gravity="center">
<ImageView android:id="@+id/chanshow_img_icon"
android:layout_width="50dp" android:layout_height="50dp"
android:layout_margin="2dp" />
</LinearLayout>
鄙人姓胡 2011-02-25
  • 打赏
  • 举报
回复
Gallery控件中可以添加边框,当时写过一点,楼主可以仿照这个处理一下试试:

ApiDemo中有一个例子:

1、声明item背景变量 int mGalleryItemBackground;
2、在适配器的构造函数中写如下代码:
public ImageAdapter(Context c) {
mContext = c;
// See res/values/attrs.xml for the <declare-styleable> that defines
// Gallery1.(此注释说明了该自定义风格的位置,在res/values/attrs.xml中)
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
/*此处obtainStyledAttributes方法如果报错,The method obtainStyledAttributes(int[]) is undefined for the type ImageAdapter,做如下修改*/
//TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
3、在getView方法中添加一行代码即可:
imageView.setBackgroundResource(mGalleryItemBackground);

如此便为Gallery每个item添加了边框。
feifei694893239 2011-02-25
  • 打赏
  • 举报
回复
4楼强人啊
dinjay 2011-02-25
  • 打赏
  • 举报
回复
既然已经设置了background,那去掉android:src= "#FFFF0000 " 不就行了么
jerrypiaopiao 2011-02-24
  • 打赏
  • 举报
回复
直接用android SDK自带的draw9patch工具做一个中间是空白的有边框的xxx.9.png图片用作ImageView的背景就可以了。
yyy025025025 2011-02-24
  • 打赏
  • 举报
回复
三个方法:

1. android:src= "#FFFF0000 " 中前面2个改为 "#33FF0000" 设置一定的透明度,这样红色就不会盖住background了。

2. android:src= "@android:drawable/btn_default"
android:background= "#FFFF0000"
android:scaleType= "fitCenter"

这边需要注意的是src中的drawable的长宽需要比ImageView长宽至少各小4px。

3. 继承ImageView实现它的onDraw函数。

个人倾向于使用第三种方法。
JiuJie_ 2011-02-24
  • 打赏
  • 举报
回复
要么自己定义一个view,要么就用一个带边框图片也挺好的呀。
梦_枫 2011-02-24
  • 打赏
  • 举报
回复
在ImageView的上下左右都加个View就行了,
view控制好view的高宽顡色就好了
小裴同学 2011-02-24
  • 打赏
  • 举报
回复
没用过这个功能,我觉得这样还不如直接做张有边框的图片。。

80,337

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧