Android learning notes-TextView (text box) (1), android-textview

Source: Internet
Author: User

Android learning notes-TextView (text box) (1), android-textview
Reference from: http://www.runoob.com/w3cnote/android-tutorial-textview.html

 

1. Basic attributes:
  • Id:Set a component id for TextView. Based on the id, we can get this object through the findViewById () method in Java code, set relevant properties, or use RelativeLayout, id is used for Reference Components!
  • Layout_width:The width of the component. Generally, the value is ** wrap_content ** or ** match_parent (fill_parent) **. The former is the size of the content displayed by the control and the size of the control, the latter will fill the parent container where the control is located; of course, it can also be set to a specific size. For example, in order to display the effect, it is set to 200dp.
  • Layout_height:The width of the widget. The content is the same as the preceding one.
  • Gravity:Set the alignment direction of the content in the control, text in TextView, and image in ImageView.
  • Text:Set the displayed text content. Generally, we write the string to string. in the xml file, and then get the corresponding String content through @ String/xxx. I will write it directly to "" For convenience. It is not recommended to write it like this !!!
  • TextColor:Set the font color. Same as above, it is referenced through colors. xml resources. Do not write it like this!
  • TextStyle:Set the font style. Three Optional values: ** normal ** (no effect), ** bold ** (bold), and ** italic ** (italic)
  • TextSize:Font size, in the unit of sp!
  • Background:The background color of the widget. It can be understood as the color of the entire widget, or an image!
2. Development example 2.1 shadow TextView

The following attributes are involved:

  • Android: shadowColor:Set the shadowRadius color. It must be used with shadowRadius!
  • Android: shadowRadius:Set the Blur degree of the shadow to 0.1 and the font color. We recommend that you use 3.0.
  • Android: shadowDx:Sets the offset of the shadow in the horizontal direction, that is, the abscissa position starting from the shadow in the horizontal direction.
  • Android: shadowDy:Sets the offset of the shadow in the vertical direction, that is, the ordinate position at the beginning of the shadow in the vertical direction.
<TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerInParent = "true" android: shadowColor = "# F9F900" android: shadowDx = "10.0" android: shadowDy = "10.0" android: shadowRadius = "3.0" android: text = "shadow TextView" android: textColor = "# 4A4AFF" android: textSize = "30sp"/>
2.2 TextView with border:

If you want to set a border background for TextView, a normal rectangular border or a rounded border! The following may help you! In addition, TextView is the parent class of many other controls, such as Button. You can also set a border like this! The implementation principle is very simple. Compile a ShapeDrawable resource file by yourself! Then TextView sets blackgroung as the drawable resource!

Briefly describe the nodes and attributes of the shapeDrawable resource file:

  • <SolidAndroid: color = "xxx"> set the background color.
  • <StrokeAndroid: width = "xdp" android: color = "xxx"> set the Border width and border color.
  • <PaddingAndroidLbottom = "xdp"...> This setting sets the margin.
  • <CornersAndroid: topLeftRadius = "10px"...> This is the rounded corner.
  • <Gradient> This is set to gradient. The optional attributes include:StartColor: Starting colorEndColor: End colorCenterColor: Intermediate colorAngle: Direction angle. If it is equal to 0, it is switched from left to right, and then counterclockwise. When angle is 90 degrees, it goes from bottom to top.Type: Set the Gradient Type
<? Xml version = "1.0" encoding = "UTF-8"?> <Shape xmlns: android = "http://schemas.android.com/apk/res/android"> <! -- Set a black border --> <stroke android: width = "2px" android: color = "#000000"/> <! -- Gradient --> <gradient android: angle = "270" android: endColor = "# C0C0C0" android: startColor = "# FCD209"/> <! -- Set the margin to make the space bigger --> <padding android: left = "5dp" android: top = "5dp" android: right = "5dp" android: bottom = "5dp"/> </shape>
2.3 TextView with image (drawableXxx:

Basic usage:

The core of image setting is:DrawableXxxYou can set images in four directions:DrawableTop(On ),DrawableButtom(Lower ),DrawableLeft(Left ),DrawableRight(Right) You can also useDrawablePaddingTo set the spacing between the image and text!

(Set images in four directions)

Implementation Code:

<RelativeLayout 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 = "com. jay. example. test. mainActivity "> <TextView android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_centerInParent =" true "android: drawableTop =" @ drawable/show1 "android: drawableLeft = "@ drawable/show1" android: drawableRight = "@ drawable/show1" android: drawableBottom = "@ drawable/show1" android: drawablePadding = "10dp" android: text = "egg"/> </RelativeLayout>
 

Some questions:You may find that the drawable we set in this way cannot be set by yourself, but cannot be set directly in XML; so we need to make a modification in the Java code!

The sample code is as follows:

Package com. jay. example. test; import android. app. activity; import android. graphics. drawable. drawable; import android. OS. bundle; import android. widget. textView; public class MainActivity extends Activity {private TextView txtZQD; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); txtZQD = (TextView) findViewById (R.id.txt ZQD); Drawable [] drawable = txtZQD. getCompoundDrawables (); // The following table of the array is 0 ~ 3, followed by drawable [1] in the upper left and lower right. setBounds (100, 0,200,200); txtZQD. setCompoundDrawables (drawable [0], drawable [1], drawable [2], drawable [3]);}
 

Run:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.