为什么我在TextView中的文本不会改变颜色?
问题描述:
我遇到了一些固定不会改变颜色的文本视图问题。我不确定它为什么不起作用。为什么我在TextView中的文本不会改变颜色?
我有我的resource/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#FDCDB2</color>
<color name="colorPrimaryDark">#E2896B</color>
<color name="colorAccent">#FF4081</color>
<color name="mainBackground">#F6F5F5</color>
</resources>
颜色我已经尝试都硬编码在layout.xml文件中的色彩以及动态设置颜色:
descriptionHeader.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
和
android:textColor="@color/colorPrimaryDark"
注意图像中带有棕色色调的文字,即所需的颜色。 “说明”,它的身体,“指令”已经决定只是一个浅灰色
下面是我的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<!--make whole layout scrollable-->
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical">
<!--nested view can only have one child-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:textColor="@color/colorPrimary"
>
<!-- main image of the brew method-->
<ImageView
android:id="@+id/topImageIV"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center|top"
android:background="@color/colorPrimary"
android:focusableInTouchMode="true" />
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--layout for the brew parameteres-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/brew_method_parameters"
android:orientation="horizontal"
android:padding="8dp"
android:weightSum="3">
<!--layout for the weight-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:orientation="vertical">
<!--top row of the weight includes image-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:orientation="horizontal">
<!--weight image icon-->
<ImageView
android:id="@+id/weight_icon_IV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:focusableInTouchMode="true"
android:src="@drawable/ic_weight" />
<!--weight parameter set dynamically-->
<TextView
android:id="@+id/TV_servingDose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:hint="16 g"
android:textColor="@color/colorPrimaryDark" />
</LinearLayout>
<!--serving size set dynamically-->
<TextView
android:id="@+id/TV_servingNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="1 Serving"
android:paddingLeft="12dp"
android:textColor="@color/colorPrimaryDark" />
</LinearLayout>
<!--layout for time requirement-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:orientation="vertical">
<!--top row of time requirement-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:orientation="horizontal">
<!--time icon-->
<ImageView
android:id="@+id/timer_icon_IV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:focusableInTouchMode="true"
android:src="@drawable/ic_clock" />
<!--brew time set dynamically-->
<TextView
android:id="@+id/TV_timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="4:00"
android:textColor="@color/colorPrimaryDark" />
</LinearLayout>
<!--brew time label-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="15dp"
android:text="Brew Time"
android:textColor="@color/colorPrimaryDark" />
</LinearLayout>
<!--layout for grind size-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:orientation="vertical">
<!--top row of grind size includes image-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:orientation="horizontal">
<!--icon for grind-->
<ImageView
android:id="@+id/grind_icon_IV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:focusableInTouchMode="true"
android:src="@drawable/ic_grinder" />
<!--grind description set dynamically-->
<TextView
android:id="@+id/TV_grindSetting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="@string/grind_size_coarse"
android:textColor="@color/colorPrimaryDark" />
</LinearLayout>
<!--grind setting description-->
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:paddingLeft="12dp"
android:text="@string/grind_setting"
android:textColor="@color/colorPrimaryDark" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<!--description of brewmethod layout-->
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical"
app:cardCornerRadius="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/mainBackground"
android:orientation="vertical">
<!--description label-->
<TextView
android:id="@+id/description_header_TV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/brew_method_description"
android:paddingStart="16dp"
android:textColor="@color/colorPrimaryDark"
android:textSize="24sp" />
<!--description text-->
<TextView
android:id="@+id/brew_method_description_TV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/bio_aeropress"
android:paddingBottom="8dp"
android:paddingRight="16dp"
android:paddingStart="16dp"
android:textColor="@color/colorPrimaryDark" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!--instructions label-->
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical"
app:cardCornerRadius="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/mainBackground"
android:orientation="vertical">
<TextView
android:id="@+id/brew_method_instructions_title_TV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorPrimaryDark"
android:hint="@string/brew_method_instructions"
android:paddingStart="16dp"
android:textSize="24sp" />
<!--instuctions recyclerview. set using InstructionListAdapter-->
<android.support.v7.widget.RecyclerView
android:id="@+id/rvInstructions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="0dp">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:titleTextColor="@color/colorPrimaryDark" />
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:backgroundTint="@color/colorPrimaryDark"
app:srcCompat="@android:drawable/ic_lock_idle_alarm" />
</android.support.design.widget.CoordinatorLayout>
答
想通了事情的原委:
与颜色问题我的文本视图,我用
android:hint="@string/brew_method_description
代替
android:text="@string/brew_method_description
开关设置的实际文本启用的颜色变化。
也许你的** android:theme **对此有一些影响。 – statosdotcom
@statosdotcom我在上面的编辑中包含了'values/styles.xml'。风格不参考那些灰色阴影 –
好的,谢谢。你的清单指向** AppTheme **,对吗?好。那么是时候重写你的主题来获得你所需要的东西了。为你的TextView创建一个主题,因为它似乎有一些超出你的xml禁止获得所需的颜色。也许硬编码你的主题,你找到自己的方式。 (长期居住的cofee爱好者) – statosdotcom