<EditTextandroid:id="@+id/edit_message"android:layout_width="wrap_content"android:layout_height="wrap_content"android:hint="@string/edit_message"/>

android:id="@+id/edit_message"

This provides a unique identifier for the view, which you can use to reference the object from your app code

The at sign (@) is required when you're referring to any resource object from XML. It is followed by the resource type (id in this case), a slash, then the resource name (edit_message).

The plus sign (+) before the resource type is needed only when you're defining a resource ID for the first time.


控件的android:id属性设置的三种方式

android:id="@+id/edit_message"

表示在R.java文件里面新增一个id为edit_message的控件索引,最常用的一种声明控件id的方式。

android:id="@android:id/tabhost"

表示引用的是系统已有的ID,在对应的sdk目录下的ids.xml里面。一般外部不去调用,是组件内部调用的时候使用。

android:id="@id/btn"

表示引用一个已经存在的ID,在R.java里面的,比如我们自己建了一个ids.xml,里面声明了一组id,其中一个是btn,那么你就可以这样引用了。


android:hint="@string/edit_message"

This is a default string to display when the text field is empty.


resource objects

A resource object is simply a unique integer name that's associated with an app resource, such as a bitmap, layout file, or string.

Every resource has a corresponding resource object defined in your project's gen/R.java file. You can use the object names in the R class to refer to your resources, such as when you need to specify a string value for the android:hint attribute. You can also create arbitrary resource IDs that you associate with a view using the android:id attribute, which allows you to reference that view from other code.

The SDK tools generate the R.java each time you compile your app. You should never modify this file by hand.


String Resources

When you need to add text in the user interface, you should always specify each string as a resource. String resources allow you to manage all UI text in a single location, which makes it easier to find and update text. Externalizing the strings also allows you to localize your app to different languages by providing alternative definitions for each string resource.

By default, your Android project includes a string resource file at res/values/strings.xml. Add a new string named "edit_message" and set the value to "Enter a message." (You can delete the "hello_world" string.)

While you're in this file, also add a "Send" string for the button you’ll soon add, called "button_send".

The result for strings.xml looks like this:

<?xmlversion="1.0"encoding="utf-8"?><resources><stringname="app_name">MyFirstApp</string><stringname="edit_message">Enteramessage</string><stringname="button_send">Send</string><stringname="action_settings">Settings</string><stringname="title_activity_main">MainActivity</string></resources>


ViewGroup

Illustration of how ViewGroup objects form branches in the layout and contain other View objects.

public abstract classViewGroup

A ViewGroup is a special view that can contain other views (called children.) The view group is the base class for layouts and views containers. This class also defines the ViewGroup.LayoutParams class which serves as the base class for layouts parameters.

see ViewGroup.LayoutParams for layout attributes.


Layouts

A layout defines the visual structure for a user interface, such as the UI for an activity or app widget. You can declare a layout in two ways:

  • Declare UI elements in XML. Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts.

  • Instantiate layout elements at runtime. Your application can create View and ViewGroup objects (and manipulate their properties) programmatically.


示例Layout

<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><TextViewandroid:id="@+id/text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello,IamaTextView"/><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello,IamaButton"/></LinearLayout>


Layout Parameters

示例

Visualization of a view hierarchy with layout parameters associated with each view.

XML layout attributes named layout_something define layout parameters for the View that are appropriate for the ViewGroup in which it resides.

例如:

‍‍android:layout_width="match_parent"

android:layout_height="match_parent"‍‍

All view groups include a width and height (layout_width and layout_height), and each view is required to define them. Many LayoutParams also include optional margins and borders.

you will use one of these constants to set the width or height:

  • wrap_content tells your view to size itself to the dimensions required by its content

  • fill_parent (renamed match_parent in API Level 8 and higher) tells your view to become as big as its parent view group will allow.



更多相关文章

  1. android之【部分控件】
  2. Android~~布局Layout
  3. Google Android(安卓)文档笔记-Training-Getting started [1]
  4. Android(安卓)Studio的build.gradle里面的各种版本信息
  5. 一点见解: Android事件分发机制(一)
  6. OpenCV在Android平台上的应用
  7. android之buttonBar的设计--style的引用
  8. android控件的对齐方式
  9. Android(安卓)UI控件之ToggleButton、Switch

随机推荐

  1. android 图片阅读 之 穹の思念
  2. Android(安卓)相机Camera API 使用
  3. AndroidUI设计之 布局管理器 - 详细解析
  4. Android 面试精华题目总结
  5. android:layout_alignParent 布局相对于
  6. Android GPS定位的简单应用
  7. Android系统深度游
  8. Android之UI设计TextView属性
  9. [Android]电话拨号器开发
  10. 学习Android界面设计的超级利器Hierarchy