Drawable资源--ShapeDrawable资源

本文介绍了Android中Shape Drawable的使用,包括rectangle、gradient_rectangle和oval三种形状的定义,详细讲解了如何设置填充颜色、边框宽度、渐变效果、内边距以及圆角。同时,展示了如何在activity_main.xml布局文件中应用这些Shape Drawable,为EditText和TextView提供背景样式。

rectangle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">
<corners android:radius="6dp"/>

<!-- 设置填充颜色 -->
    <solid android:color="#fff"/>
    
    <!-- 设置四周的内边距 -->
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp"/>
    
    <!-- 设置边框 -->
    <stroke android:width="3dp" android:color="#000"/>
</shape>

gradient_rectangle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
   <!-- 定义填充渐变颜色 -->
   <gradient 
       android:startColor="#ff0"
       android:endColor="#00f"
       android:type="linear"/>
   
   <!-- 设置圆角矩形 -->
   <corners android:radius="4dp"/>
   
</shape>

oval.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <!-- 定义填充渐变颜色 -->
    <gradient 
        android:startColor="#ff0"
        android:endColor="#00f"
        android:angle="45"
        android:type="sweep"/>
    
    <!-- 设置内填充 -->
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp"/>
    
    <!-- 设置圆角矩形 -->
    <corners android:radius="8dp"/>

</shape>

以上三个文件建立在drawable-mdpi文件夹下

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    android:orientation="vertical">


    <EditText android:id="@+id/recetEdit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/rectangle"/>
    
    
    <TextView android:id="@+id/rectGradient"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/gradient_rectangle"/>
    
    <TextView android:id="@+id/oval"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/oval"/>
    
</LinearLayout>

Main.java

package com.example.shapedrawabletest;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);




}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值