有两种方法可以这样做:
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?"
/>
您使用一种选择还是另一种选择取决于您的需求。如果您唯一想做的就是修改外观,那么最好的方法就是第一种。另一方面,如果要更改外观并向小部件添加一些新功能,则第二种方法是解决方法。