Android shape实现控件圆角,背景,边框等属性

接下来就是shape的讲解:

[html] view plain copy

  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:shape="rectangle"  
  4.     ><!-- 其中rectagle表示矩形,oval表示椭圆,line表示水平直线,ring表示环形 -->  
  5.     <!-- 节点属性介绍如下 -->  
  6.     <corners />  
  7.     <!-- 节点1:corners (圆角)  
  8.         android:radius  圆角的半径 值越大角越圆    
  9.         android:topLeftRadius   左上圆角半径    
  10.         android:topRightRadius  右上圆角半径  
  11.         android:bottomLeftRadius    左下圆角半径  
  12.         android:bottomRightRadius   右下圆角半径  
  13.     -->  
  14.       
  15.     <gradient />  
  16.     <!-- 节点2: gradient (背景颜色渐变)  
  17.         android:startColor 起始颜色  
  18.         android:centerColor 中间颜色  
  19.         android:endColor 末尾颜色  
  20.         android:angle 渐变角度,必须为45的整数倍。  
  21.         android:type 渐变模式 默认是linear(线性渐变)radial(环形渐变)  
  22.         android:centerX X坐标  
  23.         android:centerY Y坐标  
  24.         android:gradientRadius radial(环形渐变)时需指定半径  
  25.     -->  
  26.       
  27.     <padding />  
  28.     <!-- 节点3: padding (定义内容离边界的距离)  
  29.         android:left 左部边距  
  30.         android:top 顶部边距  
  31.         android:right 右部边距  
  32.         android:bottom  底部边距  
  33.     -->  
  34.       
  35.     <size />  
  36.     <!-- 节点4:size (大小)  
  37.         android:width 指定宽度  
  38.         android:height 指定高度  
  39.      -->  
  40.        
  41.     <solid />  
  42.     <!-- 节点5:solid (填充)   
  43.         android:color   指定填充的颜色  
  44.     -->  
  45.       
  46.     <stroke />  
  47.     <!-- 节点6:stroke (描边)   
  48.         android:width 描边的宽度  
  49.         android:color 描边的颜色  
  50.         把描边弄成虚线的形式"- - -":  
  51.         android:dashWidth 表示'-'这样一个横线的宽度  
  52.         android:dashGap 表示之间隔开的距离  
  53.     -->  
  54.       
  55. </shape></span> 

 

 

shape_rectangle_button.xml代码:

[html] view plain copy

  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:shape="rectangle"  
  4.     >  
  5.     <corners   
  6.         android:radius="10dip"  
  7.         />  
  8.     <solid   
  9.         android:color="#8DEEEE"  
  10.         />  
  11.     <size   
  12.         android:width="250dip"  
  13.         android:height="25dip"  
  14.         />  
  15.     <stroke   
  16.         android:width="2dip"  
  17.         android:color="#000000"  
  18.         />  
  19. </shape></span> 

 

shape_oval_button.xml代码:

 

[html] view plain copy

  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:shape="oval"  
  4.     >  
  5.     <corners   
  6.         android:radius="10dip"  
  7.         />  
  8.     <solid   
  9.         android:color="#787878"  
  10.         />  
  11.     <size   
  12.         android:width="300dip"  
  13.         android:height="50dip"  
  14.         />  
  15.     <stroke   
  16.         android:width="2dip"  
  17.         android:color="#000000"  
  18.         />  
  19.   
  20. </shape></span> 

shape_layout.xml代码:

[html] view plain copy

  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:shape="rectangle"  
  4.     >  
  5.     <corners   
  6.         android:radius="10dip"  
  7.         />  
  8.     <gradient   
  9.         android:startColor="#FFC0CB"  
  10.         android:centerColor="#FF83FA"  
  11.         android:endColor="#CAFF70"  
  12.         android:angle="90"        
  13.         />  
  14.       
  15. </shape></span> 

activity_main.xml代码:

[html] view plain copy

  1. <span style="font-size:18px;"><LinearLayout 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.     android:orientation="vertical"  
  6.     android:background="@drawable/shape_layout"  
  7.     tools:context=".MainActivity" >  
  8.       
  9.     <LinearLayout   
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:orientation="vertical"  
  13.         >  
  14.             <TextView   
  15.                 android:layout_width="wrap_content"  
  16.                 android:layout_height="wrap_content"  
  17.                 android:text="按钮:"  
  18.                 android:textSize="25sp"  
  19.                 />  
  20.             <Button   
  21.                 android:layout_width="wrap_content"  
  22.                 android:layout_height="wrap_content"  
  23.                 android:text="@string/no_changed"  
  24.                 />  
  25.             <Button   
  26.                 android:layout_width="wrap_content"  
  27.                 android:layout_height="wrap_content"  
  28.                 android:layout_marginTop="10dip"  
  29.                 android:text="@string/changed_rectangle"  
  30.                 android:background="@drawable/shape_rectangle_button"  
  31.                 />  
  32.             <Button   
  33.                 android:layout_width="wrap_content"  
  34.                 android:layout_height="wrap_content"  
  35.                 android:layout_marginTop="10dip"  
  36.                 android:text="@string/changed_oval"  
  37.                 android:background="@drawable/shape_oval_button"  
  38.                 />        
  39.         </LinearLayout>  
  40.           
  41.         <LinearLayout   
  42.             android:layout_width="match_parent"  
  43.             android:layout_height="match_parent"  
  44.             android:layout_marginTop="20dip"  
  45.             android:orientation="vertical"  
  46.             >  
  47.             <TextView   
  48.                 android:layout_width="wrap_content"  
  49.                 android:layout_height="wrap_content"  
  50.                 android:text="输入框:"  
  51.                 android:textSize="25sp"  
  52.                 />  
  53.             <EditText   
  54.                 android:layout_width="wrap_content"  
  55.                 android:layout_height="wrap_content"  
  56.                 android:layout_marginTop="10dip"  
  57.                 android:hint="@string/no_changed"  
  58.                 />  
  59.             <EditText   
  60.                 android:layout_width="wrap_content"  
  61.                 android:layout_height="wrap_content"  
  62.                 android:layout_marginTop="10dip"  
  63.                 android:hint="@string/changed_rectangle"  
  64.                 android:background="@drawable/shape_rectangle_edittext"  
  65.                 />             
  66.     </LinearLayout>  
  67. </LinearLayout></span> 

详细地址:http://blog.csdn.net/joker_ya/article/details/38638481

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答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中设置控件边框效果,使其符合设计需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值