Advanced Applications of TextView (I)

Keywords: Android Java xml encoding

We know that TextView is a very common control in the actual development of android. I will not dwell on the basic attributes of TextView here, but mainly collate the advanced applications of TextView.

**

1. Adding pictures to TextView using Drawable attributes

**

We should all be familiar with this picture of Wechat. In fact, the layout of this picture is realized by using Liear Layout to nest a Linear Layout. This way of implementation is quite simple. However, in the actual development process, we will consider that multiple layouts will lead to poor performance of released applications, so we usually use Dr. The main attributes of the awable method are drawableTop (top), drawableButtom (bottom), drawableLeft (left), drawableRight (right). Next, stick out my own ugly code.

Layout file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="com.example.dreamcreationman.androidstudy.MainActivity">

    <TextView
        android:id="@+id/text"
        android:text="Android"
        android:textSize="50sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableTop="@mipmap/ic_launcher"
        android:drawableBottom="@mipmap/ic_launcher"
        android:drawableLeft="@mipmap/ic_launcher"
        android:drawableRight="@mipmap/ic_launcher"
        android:drawablePadding="10dp"/>
</RelativeLayout>

Unfortunately, the size of the image can not be set in the XML file, but can only be controlled in the java code.

java code:

package com.example.dreamcreationman.androidstudy;

import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Get textView
        textView= (TextView) findViewById(R.id.text);
        //Create a new Drawable array to store text s from left (0), top (1), right (2), bottom (3) in turn
        Drawable[] drawables=textView.getCompoundDrawables();
        //For example, modify the figure above and set the boundary, which means that the length is: from 100dp to 200dp, the width is: from 0dp above the text to 200dp!
        drawables[1].setBounds(100,100,200,200);
        //Then retrieve the settings Drawable array (refresh), no Null
        textView.setCompoundDrawables(drawables[0],drawables[1],drawables[2],drawables[3]);
    }
}

Maotai purple has realized the above style of Wechat, which is also a small optimization.

2. Identifying Link Types Using autoLink Attributes

On the basis of the above code, add an android:autoLink= "phone" to the layout file for TextView to automatically link to the corresponding default application. For this autolink attribute, we can set it by ourselves, including phone (dial), map (map), email (mail) and so on. all is included. setAutoLinkMask(Linkify.ALL) can be invoked in Java code to automatically identify protocol headers. At this time, autolink can automatically identify without writing protocol headers, but also set up for this TextView: setMovementMethod(LinkMovementMethod.getInstance()); otherwise, clicking is ineffective!

Welcome to leave a message in the comment area pointing out the mistakes. I hope to make progress with you all! _________

Thanks for watching.

Posted by efficacious on Sat, 30 Mar 2019 12:06:28 -0700