Android Study Notes (6): xml and widgets

Source: Internet
Author: User

Typographical

If there are several widgets in a layout, the last widget uses fill_parent, which fills up the remaining space. If a widget (not the last one) uses fill_parent, the subsequent widget cannot be displayed. In a sense, fill_parent can be understood as all the space left by the parent layout.

Prompts in eclipse

When we use eclipse, XML compilation will be automatically prompted, probably because we clicked the mouse or another method, and the prompt disappears. We can use the Atl ++/method, startup prompt.

Textview (Label)

Textview in Andriod, which has some attributes in XML.

Android: Typeface monospace
Android: textstyle bold italic bold | Italic
Android: textcolor # ff0000, red
Android: textsize, for example, "25px". In the size mode, PX is used sometimes, and dip is used sometimes. Px refers to pixels, and dip refers to an abstract unit based on screen density. 1dp = 1px on a display at 160 o'clock per inch. We don't need to consider whether pixels are dense and get the expected size, so we recommend using dip.
Android: gravity content, center, left, right, etc.
Android: background color, in RGB mode
Android: The singleline value is flase or ture. If it is set to false, multiple rows are allowed.

Button

This is the subclass and Lable of TextView, and is also applicable to buttons. In Andriod Study Notes (4): without using IDE to use the command line, we provide an example of using keys. XML can be simpler.

<Button
Android: Id = "@ + ID/mybutton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: onClick = "buttonClickAction"
<! -- Format: andriod: onClick = "method_name" -->
/>

In java source code, the implement interface is not required in the class and can be directly processed in public void <method_name> (View v. As follows:

......
Import android. view. View;

Public class Activity01 extends Activity
{
Public void buttonClickAction(View Button){
......
}
}

ImageView and ImageButton

This corresponds to TextView and Button, but only an Image. Below is an example of ImageView:

<ImageView
Android: id = "@ + id/mylanscape"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: adjustViewBounds = "true"
Android:Src= "@ Drawable/hdrlandscape"
<! -- Here is the source of the image. You can also specify the URI through setImageURI (). We have placed an image hdrlandscape.jpg IN res/drawable-hdpi, which can be loaded through android: src -->
/>

EditText

The following is an example:

<EditText
Android: id = "@ + id/myfield"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android:SingleLine= "False" <! -- Multiple rows -->
<! -- Android:AutoTextAutomatic spelling check
Addroid:CapitalizeThe first letter in a word is automatically capitalized, which is helpful for the city EditText.
Andriod:DigitsOnly numbers are allowed -->
/>

Checkbox

Both CheckBox and RadioBox inherit from CompoundButton, while CompoundButton inherits TextView. As defined in XML:

<Checkbox
Android: Id = "@ + ID/mycheckbox"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "this checkbox is: uncheck"
/>

Common checkcbox functions include isChecked (), setChecked (), and toggle () (changing the status, for example, changing checked to unchecked, and changing unchecked to checked .). If the status of the CheckBox changes, you need to trigger the method in the program. As follows:

Public class helloandriod extends ActivityImplements CompoundButton. OnCheckedChangeListener{

Private checkbox mycheckbox = NULL;

@ Override
Public void oncreate (bundle savedinstancestate ){
......
Mycheckbox = (CheckBox) findViewById (R. id. mycheckbox );
Mycheckbox. setOnCheckedChangeListener (this );
}

Public voidOnCheckedChanged(CompoundButton buttonView, boolean isChecked ){
Mycheckbox. setText ("This checkbox is:" + (isChecked? "Checked": "unchcked "));
}
}

RadioBox

RadioBox processing looks different from CheckBox. RadioBox usually forms a Group, and only one RadioBox in the Group is in the checked state. Process the following in XML:

<RadioGroup
Android: id = "@ + id/myradiogroup"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
<Radiobutton Android: Id = "@ + ID/radio1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Radio text one"/>
<Radiobutton Android: Id = "@ + ID/Radio2"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Radio text two"/>
<RadioButton android: id = "@ + id/radio3"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Radio Text Three"/>
</RadioGroup>

We usually operate RadioGroup. Common methods include check (), such as roup. check (R. id. radio1. id. radiobutton of radio1 is selected; clearCheck () indicates that all the choices are unchecked; getCheckedRadioButtonId () indicates that the ID of the selected radiobutton is obtained, and-1 is returned if none exist. In the following example, we add radiobox in the previous checkbox example.

Public class HelloAndriod extends Activity implementsCompoundButton. OnCheckedChangeListener, RadioGroup. OnCheckedChangeListener{
Private RadioGroup myradiogroup = null;

Public void onCreate (Bundle savedInstanceState ){
......
Myradiogroup = (RadioGroup) findViewById (R. id. myradiogroup );
Myradiogroup. setoncheckedchangelistener (this );
}

Public voidOnCheckedChanged(Compoundbutton buttonview, Boolean ischecked ){
Mycheckbox. settext ("this checkbox is:" + (ischecked? "Checked": "unchcked "));
}

Public voidOnCheckedChanged(Radiogroup group, int checkedid ){
Int radioid = myradiogroup.GetCheckedRadioButtonId();
If (radioid <0)
Mytextview. settext ("no radio button is selected ");
Else {
RadioButton rb = (RadioButton) group. findViewById (radioId );
MyTextView. setText ("radio button:" + rb. getText ());
}
}
}

View

The widgets above are all views. You can use the View Feature in XML.

For example, android: visibility = "invisible" makes the widget invisible, but retains its position. If it is "gone", the position is not reserved.

The color-related android: background can be a specific color, such as android: background = "# 0000ff" or an image, such as android: background = "@ drawable/hdrlandscape", however, the occupied position of ckeckbox must consider the image size. For TextView and Its Inheritance classes, you can use android: textColor = "#00ff00" to specify the text color. Besides, you can also use ColorStateList to set the text color in different situations.

In the res/layout/directory, we add an Android XML file button_color.xml to describe the color of the buttons in different States:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Selector xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: color = "#00ff00" android: state_focused = "true"/>
<Item android: color = "# ff0000" android: state_pressed = "true"/>
<Item android: color = "# 0000ff" android: state_pressed = "false"/>
</Selector>

These options have priorities from top to bottom. For example, if we put state_focused at the end, it does not work because the state_pressed = "false" color is executed first. Related statuses include state_pressed, button_color, state_focused, state_selected, state_active, state_checkable, state_checked, state_enabled, state_window_focused.

Then we add the following for the relevant widgets in main. xml:Android: textColor = "@ layout/button_color"

Because the View is inherited, you can use the View method, which is related to the UI: setEnabled (), isEnabled (), requestFocus (), isFocused

GetParent ()-obtains the parent container or parent widget, findViewById (), and getRootView (), which are well understood in XML.

Related links:
My Android development articles

 

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.