0x开头的颜色值和#开头的颜色值的区别以及转化

以0x开头的颜色值和#开头的颜色值都是16位进制的

他们的0x开头的颜色值一般后面跟8位例如:0xfffefefe ,0x后面跟着前2个ff一般代表透明,后面跟着的6位和

#后面跟着6位相同, 目前自己是这样大致理解的,相互可很好转化,

例如 0x : 0xfffefefe,

          #  :#fefefe

他们2个颜色是相同的

他们的用法

 #开头的比较常用在xml布局中

0x开头比较常用的代码的布局中。

下面一段例子

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.leon.color.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="按钮一"
        android:gravity="center"
        android:textColor="#fefefe"
        android:background="#FF4081"/>

    <TextView
    android:id="@+id/btn2"
    android:layout_marginTop="30dp"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:text="按钮2"
    android:gravity="center"/>


    <TextView
        android:id="@+id/btn3"
        android:layout_marginTop="30dp"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="按钮3"
        android:gravity="center"/>

</LinearLayout>
显示效果如下



代码

btn2=findViewById(R.id.btn2);
btn2.setTextColor(0xfffefefe);
btn2.setBackgroundColor(0xffFF4081);

btn3=findViewById(R.id.btn3);
btn3.setTextColor(Color.parseColor("#fefefe"));
btn3.setBackgroundColor(Color.parseColor("#FF4081"));

运行后的效果如下



按钮3 三0x 转为#的颜色的




评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值