Network Engineering 201306114136 Elegant Experiment Four

Source: Internet
Author: User

Purpose

    1. Learn how to use color and string resources in Android.
    2. Understand how size and layout resources are used in Android.

Requirements

    1. Use color resources for activity and view in project;
    2. Use string resources and understand the reference mechanism of string resources;
    3. Set the dimensions for the view components in Android;
    4. Use layout resources to place components on the interface at the specified location.

Principle

Android resource management mechanism.

Process

1. Create a project

New Android Project View

Resdemo Project View

2. Modifying string resources

Open the/res/values/strings.xml file.

Strings.xml View

Click the Add button to add a string, enter the string name and value

A new project contains three existing strings

3. New color resource color.xml

Color.xml View

Color: R.COLOR.RED_GB

String: R.STRINGS.S

Open the Res/layout/activity_main.xml file and modify the code as follows:

<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"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=". Mainactivity"Android:background="@color/QINGLV"> <TextView android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="@string/hello_world"Android:textcolor="@color/RED_BG"android:textsize="30SP"/> </RelativeLayout>

So the interface becomes the same

We let the TextView control display the specified string, and use the RED_BG color resource, the background uses QINGLV resources.

3. use of size and layout resources

Process

1. Create the project

A) Project Name:ziyuan

b) Package Name:com.example.Ziyuan

c) Activity name:mainactivity

d) Application Name::ziyuan

e) Min SDK Version:8

Create a drawing

Ziyuan Project View

2. Open Activity_main_xml start layout with a button, a textview as an example

<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"Android:paddingbottom="@dimen/activity_vertical_margin"Android:paddingleft="@dimen/activity_horizontal_margin"Android:paddingright="@dimen/activity_horizontal_margin"Android:paddingtop="@dimen/activity_vertical_margin"Android:background="@drawable/ic_launcher"Tools:context=". Mainactivity"> <Button Android:id="@+id/button1"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_alignright="@+id/textview1"Android:layout_below="@+id/textview1"Android:layout_marginright="16DP"Android:text="@string/btn"/> <TextView Android:id="@+id/textview1"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_alignparenttop="true"Android:layout_centerhorizontal="true"Android:layout_margintop="26DP"Android:text="@string/tv"android:textsize="20DP"/></relativelayout>

Layout file diagram

Post-layout

3. Sample Code

Package Com.example.ziyuan;import android.os.bundle;import android.app.activity;import android.view.Menu; Import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.TextView; Public classMainactivity extends Activity {PrivateButton btn; PrivateTextView TV; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); BTN=(Button) Findviewbyid (R.id.button1); TV=(TextView) Findviewbyid (R.ID.TEXTVIEW1); Btn.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View arg0) {//TODO auto-generated Method StubTv.settextcolor (trim_memory_moderate);    }        });} @Override Publicboolean Oncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }}

Think and Answer:

What are the units for size and size? Where are the applications applied separately?

PX: Indicates the actual pixel of the screen. For example, the 320*480 screen has 320 pixels in the landscape and 480 pixels in the portrait.

In: represents inches, which is the physical size of the screen. Equal to 2.54 centimeters per inch. For example, describe the size of a mobile phone screen, often said, 3.2 (English), 3.5 (English), 4 (English) inch refers to this unit. These dimensions are the diagonal length of the screen. If the phone's screen is 3.2 inches, the diagonal length of the phone's screen (viewable area) is 3.2*2.54 = 8.128 centimeters. Readers can measure their own phone screen to see if the actual size is the same.

MM: Represents the millimeter, which is the physical size of the screen.

PT: Represents a point, which is the physical size of the screen. Size is 1 inches of 1/72.

PX (Pixels, pixels): corresponds to the actual pixel point on the screen. For example, the 320*480 screen has 320 pixels in the landscape and 480 pixels in the portrait.

In (Inches, inches): The unit of physical length of the screen. Equal to 2.54 centimeters per inch. For example, describe the size of a mobile phone screen, often said, 3.2 (English), 3.5 (English), 4 (English) inch refers to this unit. These dimensions are the diagonal length of the screen. If the phone's screen is 3.2 inches, the diagonal length of the phone's screen (viewable area) is 3.2*2.54 = 8.128 centimeters. Readers can measure their own phone screen to see if the actual size is the same.

MM (millimeters, mm): unit of physical length of the screen.

PT (Points, LB): The screen physical length unit, which represents a point, is the physical size of the screen. Size is 1 inches of 1/72.

DP (density independent pixels): Logical unit of length, 1dp=1px=1/160 inch on the on-screen dpi. As the density changes, the corresponding number of pixels also changes, but there is no direct change in proportion.

Dip: Same as DP, more for Google samples.

SP (pixels independent of density and font scaling): Similar to DP, but can be scaled based on the user's font size preference.

Try to use DP as the space size unit, and the SP as the text-related size unit.

Experimental summary

Through this experiment, I initially mastered the use of color and string resources in Android, as well as an in-depth understanding of how to use the size and layout resources in Android.

In the use of color resources because the effect of the RGB value of the missing hit a #, so the program error, but after careful examination found the problem, added;

The initial reference string resources when the time is not skilled, a lot of errors at first, and then gradually started some;

By modifying some numerical values of the program code, we further understand how to set the dimensions for the view components in Android.

Use the layout resource to place the components on the interface in the specified location, and then, in contrast with the code, to understand the layout settings in depth.

Network Engineering 201306114136 elegant experiments four

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.