android图片设置边框

用典型shape来当背景,linearlayout套住imageview,本来用的padding,结果导致长宽padding的距离不一样,非常难看,原来直接用margin就可以了。



<LinearLayout
    android:id="@+id/linear_studentphoto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/shape_photo_bg" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       android:layout_margin="10dp"
        android:scaleType="centerCrop"
        android:src="@mipmap/baobao"
        android:id="@+id/student_photo" />

</LinearLayout>

shape里面的代码很简单,drawable里面建一个shape_photo_bg.xml,




<?xml version="1.0" encoding="utf-8"?>

<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <stroke
        android:color="@color/huang"
        android:width="15dp"/>

    <corners android:radius="10dp"/>

    <solid android:color="@color/huang"/>
</shape>




代码里面引用图片直接设置 外层linearlayout的布局参数就可以了,imageview只需要设置内容即可。

student_photo = (ImageView) view.findViewById(R.id.student_photo);
linear_studentphoto = (LinearLayout) view.findViewById(R.id.linear_studentphoto);
int photokuan = 70;
int photogao = 100;
linear_studentphoto.setLayoutParams(new LinearLayout.LayoutParams(photokuan, photogao));













  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 要在Android Studio中设置控件边框,您可以使用以下方法: 1. 在布局文件中使用XML定义控件并设置其背景属性为drawable。 例如,在TextView中设置边框: ``` <TextView android:id="@+id/myTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/my_border"/> ``` 2. 创建一个drawable资源文件并设置其形状和边框属性。 例如,创建一个名为my_border.xml的文件并设置边框属性: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:width="2dp" android:color="#FF0000" /> </shape> ``` 这将在控件周围创建一个2dp宽度的红色边框。 您还可以设置其他属性,如填充和圆角等。 请注意,在使用drawable时,您需要将其放置在res / drawable文件夹中。 ### 回答2: 在Android Studio中,可以通过以下步骤为控件设置边框: 1. 首先,在布局文件中找到需要设置边框的控件,例如一个Button。 2. 在Button的属性列表中,找到android:background属性,并将其值设置为一个drawable资源。 3. 创建一个新的drawable资源文件,可以通过右键点击res文件夹 -> New -> Drawable resource file来创建。 4. 在创建的drawable资源文件中,可以使用XML代码来定义边框样式。例如,可以使用<shape>标签来创建一个矩形形状,并设置边框的颜色、宽度和圆角等属性。 5. 在<Button>标签的android:background属性中,引用刚创建的drawable资源文件。 6. 最后,重新编译并运行应用程序,即可看到控件已经设置边框。 这是一个基本的设置控件边框的过程,通过选择不同的drawable资源文件和设置不同的属性,还可以实现更加丰富多样的边框效果。希望对你有所帮助! ### 回答3: 要在Android Studio中设置控件的边框,可以按照以下步骤进行操作: 1. 在XML布局文件中,找到要设置边框的控件,并在其外部添加一个FrameLayout或者一个RelativeLayout作为父布局。 2. 在该父布局上设置背景颜色或者背景图片,实现控件边框的效果。 3. 使用Android Studio布局编辑器中的属性面板,可以设置父布局的padding属性来调整边框的宽度。 4. 如果希望调整边框的颜色,可以在父布局的背景属性中使用shape资源文件,定义具有边框的形状,并指定边框的颜色。 5. 如果需要将边框设置为可点击状态,可以在代码中使用setClickable(true)方法,使控件响应点击事件。 6. 除了使用背景属性来设置边框,还可以使用边框属性来设置边框的样式、宽度和颜色。可以在res/values/styles.xml文件中定义自定义的样式,并将其应用到控件上。 7. 最后,使用Android Studio中提供的预览功能,可以实时查看控件边框的效果,对属性进行调整,直到满意为止。 通过以上步骤,可以在Android Studio中设置控件的边框效果,使其符合设计需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值