android 添加全局view,在Android中为视图设置全局样式

有两种方法可以这样做:

1.使用样式

您可以通过在res/values目录上创建XML文件来定义自己的样式。因此,假设您要使用红色和粗体文本,然后创建一个具有以下内容的文件:

@style/MyRedTextAppearance

#F00

bold

例如,您可以随意命名res/values/red.xml。然后,您唯一要做的就是在所需的小部件中使用该视图,例如:

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

style="@style/MyRedTheme"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="This is red, isn't it?"

/>

有关更多参考,请阅读本文:了解Android主题和样式

2.使用自定义类

这是实现此目的的另一种可能的方法,它将是提供您自己TextView的文本颜色,始终将其设置为您想要的任何颜色。例如:

import android.content.Context;

import android.graphics.Color;

import android.util.AttributeSet;

import android.widget.TextView;

public class RedTextView extends TextView{

public RedTextView(Context context, AttributeSet attrs) {

super(context, attrs);

setTextColor(Color.RED);

}

}

然后,您只需要TextView在XML文件中将其视为普通文件即可:

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="This is red, isn't it?"

/>

您使用一种选择还是另一种选择取决于您的需求。如果您唯一想做的就是修改外观,那么最好的方法就是第一种。另一方面,如果要更改外观并向小部件添加一些新功能,则第二种方法是解决方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值