Follow me Android three commonly used views

aims

Grasp the concept of views.

Understand the difference between Activity and Widget. 
Grasp the characteristics and basic characteristics of the XML layout interface. 
Grasp the usage of several common basic views and 
learn how to use code for interface layout. 

Familiar with the event-driven model of interface programs

View is a visual interface element. Any visual component needs to be inherited from the android.view.View class. View classes are usually divided into three types: layout class, view container class and basic view class. As shown below:


View is a visual interface element. The view object can be a single component or a combination of many components, and the view object can be created through XML. code show as below:


<LinearLayout android:orientation=“vertical”android:layout_width=“fill_parent”android:laout_height=“fill_parent”><TextView android:id="@+id/text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello, I am a TextView"/><Button … /></LinearLayout>

The following table lists some common attributes and methods of the View class 



XML attribute                  description

android:padding         sets the padding area for the four sides of the component

android:scrollbars defines how many scroll bars are displayed when the component scrolls. This attribute supports the following attribute values.

none: no scroll bar

horizontal: display horizontal scroll bar

vertical: display the vertical scroll bar

android:tag               sets a string tag value for the component. Then you can get the string through View's getTag(), or query the component through findViewWithTag()

android:visibility       sets whether the component is visible. There are 3 values ​​for this attribute:

visible: visible

invisible: invisible

gone: hidden

Used to set attributes related to component size

Each view needs to define android:layout_width and android:layout_height, the value can have the following situations, you can specify the exact size:

wrap_content: indicates that the content of the view determines the size

fill_parent (renamed match_parent after Level8) means the same size as the parent container

There are two ways to set the component to fill the parent container

The inner filling part exists in the component space, which is equivalent to the effect of the page margin

padding-used to set the inner margin of the four sides

paddingLeft、paddingRight、paddingTop、paddingBottom

The outer filling part exists outside the component space, similar to the boundary of the interval

margin-used to set the outer spacing of the four sides

marginLeft、marginRight、marginTop、marginBottom

The difference between view and Widget (widget) is : View is a visual component element, Widget is actually a mini Application, View can exist in Widget, View can be considered as a more basic element of the interface.

Use XML layout view

XML layout file is a common method defined in view of the Android system, the file must be saved in res / layout directory, extension XML layout files must be xml, XML file name must conform to the Java variable naming conventions, each layout file The root node can be any component. The root node of the layout file must contain the android namespace. The component label needs to use "@+id/stringvalue" to specify the ID. The ID value must conform to Java's variable naming convention.

xmlns:android="http://schemas.android.com/apk/res/android"

<TextView android:id=“@+id/textview1” …>

XML layout file example



<?xml version=“1.0” encoding=“utf-8”?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”android:orientation=“vertical” android:layout_width=“fill_parent”android:layout_height=“fill_parent”><TextView android:id=“@+id/textview1” android:layout_width=“fill_parent”android:layout_height=“wrap_content” android:text=“textview1” /><Button android:id=“@+id/button1” android:layout_width=“wrap_content”android:layout_height=“wrap_content” android:text=“ok” /></LinearLayout>


Use XML layout in Activity 


Use XML layout in code by calling setContentView() method


public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
}


Control the view in the code by calling the findViewById() method


TextView textView1 = (TextView)findViewById(R.id.textview1);
textView1.setText("new string content");


Two: basic view


Text control-TextView, display text information, display basic text.

     

<TextView android:id="@+id/textview1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="I am a text control" />

           Display complex text

   

<TextView android:id=“@+id/textview1” android:layout_width=“fill_parent”android:layout_height=“wrap_content” android:text=“我是文本控件”android:textSize=“20dp” android:textColor=“#FF0000”android:background=“#FFFFFF” android:padding=“30dp”android:layout_margin=“30dp”/>

                 Display URL information, automatically identify URL content by specifying android:autoLink attribute, none does not match any link (default value), web matches web address, email matches email address, phone matches phone number.

Example 

<TextView android:id=“@+id/urlview” android:layout_width=“fill_parent”android:layout_height=“wrap_content” android:link=“email” />


TextView urlView = (TextView)findViewById(R.id.urlview);urlView.setText(“电子邮件:[email protected]”);

编辑框----EditText 

EditText继承自TextView,可以接受用户输入,并可以设置输入的数据类型。

属性inputtype,可以限定输入数据的类型,以下是常用的设置类型:

text:可输入所有字符

textUri:  可输入网址

textEmailAddress:可输入电子邮件

textPassword:可输入密码

number:可输入0^9的数字

date:可输入日期(0-9、”/”)

time:可输入时间(0-9、“: pam”)

phone:可输入电话号码

按钮控件——Button 
         普通按钮控件的示例 

<Button android:id=“@+id/okbtn” android:layout_width=“wrap_content”android:layout_height=“wrap_content” android:text=“我是按钮” />


Guess you like

Origin blog.51cto.com/2096101/2588825