试图在android中为我的文本设置颜色

时间:2013-04-18 00:53:46

标签: android colors

我正在尝试为android中的文本设置颜色。每次我启动我的应用程序时它都会关闭。这是我在color.xml文件中的内容:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<color name="background_color">#006400</color>
<color name="app_text_color">#FFE4C4</color>

这是我在MainActivity Class中的内容:

int textColor = getResources().getColor(R.color.app_text_color);

        TextView helloText = (TextView)findViewById(R.string.hello_world);

        helloText.setTextColor(textColor);

这是布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
         />  

</RelativeLayout>

Log Car:

04-17 21:00:39.290: E/AndroidRuntime(9986):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1670)

1 个答案:

答案 0 :(得分:0)

而不是

TextView helloText = (TextView)findViewById(R.string.hello_world);

你必须在findViewByID中使用一些ID,比如

TextView helloText = (TextView)findViewById(R.id.your_textview_id);

此处your_textview_id是布局xml中textview的id ..

<TextView
    android:id="@+id/your_textview_id"
     .............
      .........

我刚试过这个,它对我有用:

TextView helloText = (TextView)findViewById(R.id.hello_world);

helloText.setTextColor(getResources().getColor(R.color.app_text_color));