自定义文本背景,圆角边框

前端之家收集整理的这篇文章主要介绍了自定义文本背景,圆角边框前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
1、创建基础的xml布局文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical">
  7. <TextView
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:text="带边框的文本"
  11. android:textSize="24pt"
  12. android:background="@drawable/bg_border"/>
  13. <TextView
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:text="圆角边框"
  17. android:textSize="24pt"
  18. android:background="@drawable/bg_border2"/>
  19.  
  20. </LinearLayout>
2、自定义背景bg_border
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">
  3. <!--设置背景为透明色-->
  4. <solid android:color="#0000"/>
  5. <stroke android:width="4px"
  6. android:color="#f00"/>
  7. </shape>

自定义背景bg_border2

  1. <?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 android:topLeftRadius="20px"
  6. android:topRightRadius="5px"
  7. android:bottomRightRadius="20px"
  8. android:bottomLeftRadius="5px"/>
  9. <!--指定边框的宽度和颜色-->
  10. <stroke android:width="4px" android:color="#f0f"/>
  11. <!--指定使用渐变背景色,使用sweep类型渐变,颜色从红色-》绿色-》蓝色-->
  12. <gradient android:startColor="#f00"
  13. android:centerColor="#0f0"
  14. android:endColor="#00f"
  15. android:type="sweep"/>
  16. </shape>
3、加载显示的Activity
  1. public class MainActivity extends Activity
  2. {
  3.  
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState)
  6. {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.layout_circle);
  9. }
  10. }

猜你在找的XML相关文章