“This is the first day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021”

preface

Today is the first day of learning Android. You have configured the environment, installed Android Studio, created a project, and prepared some basic content. Now you can’t wait to enter the amazing world of Android

The text

The first step is to learn the most basic control: display a text on the screen. The Android Sdk has provided us with a control TextView, named at a glance. When you create a project,Android Studio will create a MainActivity for you with a default TextView. Let’s take a look

Just plain text is certainly not enough for our needs, so let's look at the basic attributes.

<? The XML version = "1.0" encoding = "utf-8"? > <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:textColor="@color/teal_200" android:textSize="30dp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>Copy the code

  • Android :textColor control textColor: support hexadecimal color
  • Android :textStyle Controls text styles: plain, bold, italic
  • Android :textSize controls textSize: dp is generally used

It’s not enough just to control these properties in XML. We need to dynamically control some of these properties in our code, so we need to give our TextView a name so that our Activity can find them.

Naming words is not actually very sheol there is no big problem, everyone has everyone’s style, the problem is not big, mainly see name know meaning.

Then find it in the Activity

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = findViewById(R.id.activity_main_tv_test); }}Copy the code

Set some basic properties, from top to bottom: font size, font color, font style, font size

Textview.settext (" This is test text "); textView.setTextColor(ContextCompat.getColor(this,R.color.teal_200)); textView.setTypeface(Typeface.DEFAULT_BOLD); textView.setTextSize(14);Copy the code

Let's take a look at a new feature that matches images with text

Insert the following code into your XML

android:drawableBottom="@drawable/ic_launcher_background"
Copy the code

The downside of this is that we have no control over the size of the image, which is not what we want. Don’t worry, there are more ways than difficulties.

Drawable Drawable = getDrawable(r.rawable. Ic_launcher_background); // set our resource object to drawable.setBounds(0,0,30,30); / / it is set to the corresponding position of our TextView TextView. SetCompoundDrawables (null, null, null, drawable);Copy the code