android method to set the color font of a control

  • 2020-05-19 05:43:08
  • OfStack

1. Use the code to set the color of the control:


    int b =  getResources().getColor(R.drawable.blue);// Get the color in the configuration file 
    mButton.setTextColor(b);

2. Set the font of the space:
Method 1: mText.setTypeface (Typeface.createFromAsset (getAssets(),"fonts/ HandmadeTypewriter.ttf "); // set the font
Note: 1. Ensure that document 1 is in ttf format; 2. Put it in assets/fonts; 3. If the corresponding font is not found, no error will be reported, but it will not be displayed when running
Method 2: fontButton. setTypeface(Typeface. defaultFromStyle(Typeface. ITALIC)); // set in an internally supported manner


package com.oyzz.ch3_6;
import android.app.Activity;
/* Must be quoted graphics.Color In order to use Color.* The object of */
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
/* Must be quoted  widget.Button To declare the use of Button object */
import android.widget.Button;
/* Must be quoted  widget.TextView To declare the use of TestView object */
import android.widget.TextView;
public class Ch3_6 extends Activity 
{
  private Button mButton;
  private TextView mText;
  private int[] mColors;
  private int colornum;
  private Button fontButton;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    /* through findViewById The constructor is used main.xml with string.xml
     In the button with textView The parameters of the */
    mButton=(Button) findViewById(R.id.mybutton);
    mText= (TextView) findViewById(R.id.mytext);
    fontButton=(Button) findViewById(R.id.mybutton1);
    /* Declare and construct 1 The integer array To store the color of the text you want to use */
    mColors = new int[] 
                      { 
    Color.BLACK, Color.RED, Color.BLUE,
    Color.GREEN, Color.MAGENTA, Color.YELLOW
                       };
    colornum=0;
    // get color.xml The color in the file 
    int b =  getResources().getColor(R.drawable.blue);// Get the color in the configuration file 
    mButton.setTextColor(b);
    /* use setOnClickListener Let the button listen to the event */
    mButton.setOnClickListener(new View.OnClickListener() 
    {             
      /* use onClick Let the user click a button to drive a change in text color */
      public void onClick(View v)
      {        
        if (colornum < mColors.length)
        {
          mText.setTextColor(mColors[colornum]);
          colornum++;
        }
        else
          colornum=0;
       }  
    });

    fontButton.setOnClickListener(new Button.OnClickListener() {
  public void onClick(View v) {
   mText.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf"));// Set the font 
   fontButton.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));// Set it up in an internally supported way 
  }
 });
  }
}

main.xml:

<?xml version="1.0" encoding="utf-8"?>

    <!-- Layout Use a white background  -->
<LinearLayout
  android:id="@+id/widget27"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/white"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  >
  <!-- 
       Text using the mytext As a � id use string.xml In the 
       the textview_str � �   All colors and text are in grey 
  -->
  <TextView
  android:id="@+id/mytext"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/textview_str"
  android:textColor="@drawable/darkgray"
  >
  </TextView>
  <!--  According to the � to mybutton As a � id use string.xml In the 
    the button_str � � 
  -->
  <Button
  android:id="@+id/mybutton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/button_str"
  >
  </Button>
   <Button
  android:id="@+id/mybutton1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text=" The font "
  >
  </Button>
</LinearLayout>

color.xml:

<?xml version="1.0" encoding="utf-8"?>
  <resources>
  <drawable name="darkgray">#404040ff</drawable>
  <drawable name="black">#000</drawable>
  <drawable name="red">#ff00ff</drawable>
  <drawable name="green">#0ff0ff</drawable>
  <drawable name="lightgray">#c0c0c0ff</drawable>
  <drawable name="white">#ffffffff</drawable>
  <drawable name="yellow">#ffFF33ff</drawable>
  <drawable name="blue">#00ffff</drawable>
  <drawable name="gray">#808080ff</drawable>
  <drawable name="magenta">#ff6699ff</drawable>
  <drawable name="cyan">#66ffffff</drawable>
</resources>

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="hello">Hello World, Ex03_13</string>
  <string name="app_name">Ex03_13</string>
  <string name="textview_str"> Turn! 7 Colorful neon lights </string>
  <string name="button_str"> According to my </string>
</resources>


Related articles: