Android:Drawable样式和尺寸单位

本文详细介绍了Android中的Drawable样式,包括ColorDrawable、NinepatchDrawable、ShapeDrawable、GradientDrawable、BitmapDrawable等,以及Android尺寸单位的概念,如dp单位的使用和不同屏幕尺寸的图片适配。
摘要由CSDN通过智能技术生成

3.6 Drawable样式装饰

1、ColorDrawable

ColorDrawable:在res/values/color.xml中添加颜色值,然后@color/颜色名获取资源;

示例:color.xml

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

<resources>

    <color name="colorPrimary">#008577</color>

    <color name="colorPrimaryDark">#00574B</color>

    <color name="colorAccent">#D81B60</color>

    <!--十六进制颜色值-->

    <color name="LightPink">#FFB6C1</color>

    <!--一般颜色值是十六进制6位,如果加入透明度,00~FF加入开始两位-->

    <color name="BlackFF">#FF000000</color>

</resources>

XML布局文件中使用颜色

示例:

<!--java中设置颜色-->

<TextView

    android:id="@+id/tv_1"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:text="Hello World!"

    android:textSize="20sp"/>

<TextView

    android:id="@+id/tv_2"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:text="Hello World!"

    android:textSize="20sp"/>

<!--使用color.xml中颜色-->

<TextView

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:text="Hello World!"

    android:textSize="20sp"

    android:textColor="@color/colorAccent"/>


<!--使用android自带颜色-->

<TextView

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:text="Hello World!"

    android:textSize="20sp"

    android:textColor="@android:color/holo_orange_dark"/>

Activity中设置颜色

示例:

public class ColorDrawableActivity extends Activity {

        //上下文

        private Context mContext;

        private TextView textView;

        private TextView textView1;

        @Override

        protected void onCreate( Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);

                setContentView(R.layout.learn_color_drawable);

                mContext = ColorDrawableActivity.this;

                textView = findViewById(R.id.tv_1);

                textView1=findViewById(R.id.tv_2);

                //设置颜色,自定义,需要0x开头,透明度,6位颜色值

                int color = 0xff008577;

                textView.setBackgroundColor(color);

                //获取color.xml中颜色

                int color1=getResources().getColor(R.color.colorAccent);

                //获取系统颜色

                int color2= Resources.getSystem().getColor(android.R.color.holo_blue_dark);

                textView1.setBackgroundColor(color2);

        }

}
 

2、NinepatchDrawable

NinepatchDrawable:点9图,图片:图片名.9.png等,使用NinePatchEditor工具编辑获得图片资源;

示例:drawable下创建xml资源文件

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

<!--

    点9图drawable资源

-->

<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"

android:src="@drawable/icon_v"

android:dither="true">

</nine-patch>

Layout布局文件使用.9图

<ImageView

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:src="@drawable/draw_ninepatch_1"/>

3、ShapeDrawable

ShapeDrawable:形状Drawable资源,包括直线,边框,环形,方形,椭圆等;

① <shape>:

 ~ visible:设置是否可见

 ~ shape:形状,可选:rectangle(矩形,包括正方形),oval(椭圆,包括圆),line(线段),ring(环形)

 ~ innerRadiusRatio:当shape为ring才有效,表示环内半径所占半径的比率,如果设置了innerRadius, 他会被忽略

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偶是不器

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值