TextView 设定颜色

本文介绍如何在Android应用中使用TextView显示带有链接的文本并调整文本颜色。通过示例展示了如何利用XML布局文件配置TextView的基本属性,如宽度、高度、自动链接等,并通过Java代码设置TextView的背景颜色及文本颜色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CharSequence str=getString(R.String.str);

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:autoLink="all"
    android:text="百度:http://www.baidu.com"
    />

 

TextView tv=(TextView)findViewById(R.id.mytextview);

tv.setText();

 

color.xml

<?xml version="1.0" encoding="UTF-8"?>
<resources>
<drawable name="white">#ffffff</drawable>
<drawable name="black">#000000</drawable>
<drawable name="mid">#C2CE99</drawable>
</resources>

 

mail.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@drawable/white"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
  <TextView android:id="@+id/lable"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@drawable/black"
    android:text="@string/height"
    />

</LinearLayout>

通过java代码来改变颜色

Resources resources=getBaseContext().getResources();
    Drawable white=resources.getDrawable(R.drawable.mid);

    TextView tv=(TextView)findViewById(R.id.lable);
    tv.setBackgroundDrawable(white);
    tv.setTextColor(Color.RED);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值