如何使TextView的文本使用我给它的样式?

时间:2019-07-09 10:39:52

标签: android text textview styles

在Android TextView上,如何使文本▶ Play使用我指定的样式。问题在于文本。在模拟器上,它可以正常显示,但是在设备上进行测试时,它会显示蓝色和白色文本。

以下代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    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"
    tools:context=".TestingActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="▶ Play"/>

</androidx.constraintlayout.widget.ConstraintLayout>

产生:

Screenshot

并且:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    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"
    tools:context=".TestingActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_red_light"
        android:textColor="@android:color/black"
        android:text="▶ Play"/>

</androidx.constraintlayout.widget.ConstraintLayout>

产生:

enter image description here

2 个答案:

答案 0 :(得分:0)

这是为您提供的解决方案... 只需在文本视图中使用android:drawableLeft="@drawable/your_icon"

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    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"
    tools:context=".TestingActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_red_light"
        android:textColor="@android:color/black"
        android:drawableLeft="@drawable/your_icon"
        android:text="Play"/>

</androidx.constraintlayout.widget.ConstraintLayout>

答案 1 :(得分:0)

You can drawableStart to set an icon before text in Textview
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:drawableStart="@drawable/play_button_icon"
    android:text="Play"/>

 To download icon use : https://materialdesignicons.com/