linerlayout 加边框

1.首先在res目录下新建一个xml文件,类型选择drawable,将自动生一个一个drawable文件(我用的sdk是android 4.1),并生成一个xml文件,在其中写入以下代码:

[java]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <solid android:color="#FFFFFF" />  
  5.   
  6.     <stroke  
  7.         android:width="0.01dp"  
  8.         android:color="#FFFFFF" />  
  9.   
  10.     <padding  
  11.         android:bottom="1dp"  
  12.         android:left="0.5dp"  
  13.         android:right="0.5dp"  
  14.         android:top="0dp" />  
  15. </shape>  

2.在要设置边框的控件xml命令里加入:android: background=“@drawable/boder
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要为 ImageView 边框,你可以使用以下方法: 1. 使用 xml 定义边框样式 在 res/drawable 目录下创建一个 xml 文件,例如 border.xml,内容如下: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@android:color/transparent" /> <stroke android:width="2dp" android:color="@color/border_color" /> </shape> ``` 然后在布局文件中设置 ImageView 的背景为该 xml 文件: ```xml <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/border" /> ``` 2. 使用代码设置边框样式 在代码中使用 ShapeDrawable 类创建边框样式,并设置给 ImageView 的背景: ```java int borderWidth = 2; // 边框宽度,单位为像素 int borderColor = getResources().getColor(R.color.border_color); // 边框颜色 ShapeDrawable shapeDrawable = new ShapeDrawable(); shapeDrawable.setShape(new RectShape()); shapeDrawable.getPaint().setColor(Color.TRANSPARENT); // 设置背景颜色为透明 shapeDrawable.getPaint().setStyle(Paint.Style.STROKE); // 设置画笔样式为描边 shapeDrawable.getPaint().setStrokeWidth(borderWidth); // 设置描边宽度 shapeDrawable.getPaint().setStrokeCap(Paint.Cap.ROUND); // 设置描边端点形状为圆形 shapeDrawable.getPaint().setStrokeJoin(Paint.Join.ROUND); // 设置描边连接处形状为圆形 shapeDrawable.getPaint().setAntiAlias(true); // 开启抗锯齿 ImageView imageView = findViewById(R.id.image); imageView.setBackground(shapeDrawable); // 设置背景为 shapeDrawable ``` 以上两种方法都可以实现为 ImageView 边框的效果,选择哪种方法取决于你的实际需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值