android--View 你应该记住的一些事

[1].A View occupies a rectangular area on the screen and is responsible for drawing and event handling. View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). The ViewGroup subclass is the base class for layouts, which are invisible containers that hold other Views (or other ViewGroups) and define their layout properties.


[2].All of the views in a window are arranged in a single tree. You can add views either from code or by specifying a tree of views in one or more XML layout files.


[3].Note: The Android framework is responsible for measuring, laying out and drawing views. You should not call methods that perform these actions on views yourself unless you are actually implementing a ViewGroup.


[4].View IDs need not be unique throughout the tree, but it is good practice to ensure that they are at least unique within the part of the tree you are searching.


[5].The geometry of a view is that of a rectangle. A view has a location, expressed as a pair of left and top coordinates, and two dimensions, expressed as a width and a height. The unit for location and dimensions is the pixel.


[6].It is possible to retrieve the location of a view by invoking the methods getLeft() and getTop(). The former returns the left, or X, coordinate of the rectangle representing the view. The latter returns the top, or Y, coordinate of the rectangle representing the view. These methods both return the location of the view relative to its parent. For instance, when getLeft() returns 20, that means the view is located 20 pixels to the right of the left edge of its direct parent.


[7].The size of a view is expressed with a width and a height. A view actually possess two pairs of width and height values. 

The first pair is known as measured width and measured height. These dimensions define how big a view wants to be within its parent (see Layout for more details.) The measured dimensions can be obtained by calling getMeasuredWidth() and getMeasuredHeight().

The second pair is simply known as width and height, or sometimes drawing width and drawing height. These dimensions define the actual size of the view on screen, at drawing time and after layout. These values may, but do not have to, be different from the measured width and height. The width and height can be obtained by calling getWidth()and getHeight().


[8].Layout is a two pass process: a measure pass and a layout pass. The measuring pass is implemented in measure(int, int) and is a top-down traversal of the view tree. Each view pushes dimension specifications down the tree during the recursion. At the end of the measure pass, every view has stored its measurements. The second pass happens inlayout(int, int, int, int) and is also top-down. During this pass each parent is responsible for positioning all of its children using the sizes computed in the measure pass.


[9].The measure pass uses two classes to communicate dimensions. 

The View.MeasureSpec class is used by views to tell their parents how they want to be measured and positioned. 

MeasureSpecs are used to push requirements down the tree from parent to child. A MeasureSpec can be in one of three modes:

  • UNSPECIFIED: This is used by a parent to determine the desired dimension of a child view. For example, a LinearLayout may call measure() on its child with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how tall the child view wants to be given a width of 240 pixels.
  • EXACTLY: This is used by the parent to impose an exact size on the child. The child must use this size, and guarantee that all of its descendants will fit within this size.
  • AT_MOST: This is used by the parent to impose a maximum size on the child. The child must gurantee that it and all of its descendants will fit within this size.

The base LayoutParams class just describes how big the view wants to be for both width and height. For each dimension, it can specify one of:

  • an exact number
  • MATCH_PARENT, which means the view wants to be as big as its parent (minus padding)
  • WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content (plus padding).

To initiate a layout, call requestLayout(). This method is typically called by a view on itself when it believes that is can no longer fit within its current bounds.


[10].Drawing is handled by walking the tree and rendering each view that intersects the invalid region. Because the tree is traversed in-order, this means that parents will draw before (i.e., behind) their children, with siblings drawn in the order they appear in the tree. If you set a background drawable for a View, then the View will draw it for you before calling back to its onDraw() method.

To force a view to draw, call invalidate().

[11].The basic cycle of a view is as follows:

  1. An event comes in and is dispatched to the appropriate view. The view handles the event and notifies any listeners.
  2. If in the course of processing the event, the view's bounds may need to be changed, the view will call requestLayout().
  3. Similarly, if in the course of processing the event the view's appearance may need to be changed, the view will call invalidate().
  4. If either requestLayout() or invalidate() were called, the framework will take care of measuring, laying out, and drawing the tree as appropriate.

Note: The entire view tree is single threaded. You must always be on the UI thread when calling any method on any view. If you are doing work on other threads and want to update the state of a view from that thread, you should use a Handler.


[12].Unlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view. They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中实现登录界面记住密码的功能,可以通过使用SharedPreferences来保存用户的登录信息。 首先,在登录界面的布局文件中,添加一个复选框用于用户选择是否记住密码。如: ```xml <CheckBox android:id="@+id/rememberPasswordCheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码" /> ``` 然后,在登录按钮的点击件中,判断记住密码复选框的状态。如果选择了记住密码,则将用户名和密码保存到SharedPreferences中。代码如下: ```java Button loginButton = findViewById(R.id.loginButton); loginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EditText usernameEditText = findViewById(R.id.usernameEditText); EditText passwordEditText = findViewById(R.id.passwordEditText); CheckBox rememberPasswordCheckBox = findViewById(R.id.rememberPasswordCheckBox); String username = usernameEditText.getText().toString(); String password = passwordEditText.getText().toString(); if (rememberPasswordCheckBox.isChecked()) { SharedPreferences preferences = getSharedPreferences("loginPrefs", MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putString("username", username); editor.putString("password", password); editor.apply(); } // 登录操作... } }); ``` 在登录界面的`onCreate()`方法中,通过读取SharedPreferences中保存的用户名和密码,并设置到对应的EditText中。代码如下: ```java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); EditText usernameEditText = findViewById(R.id.usernameEditText); EditText passwordEditText = findViewById(R.id.passwordEditText); CheckBox rememberPasswordCheckBox = findViewById(R.id.rememberPasswordCheckBox); SharedPreferences preferences = getSharedPreferences("loginPrefs", MODE_PRIVATE); String savedUsername = preferences.getString("username", ""); String savedPassword = preferences.getString("password", ""); usernameEditText.setText(savedUsername); passwordEditText.setText(savedPassword); rememberPasswordCheckBox.setChecked(!TextUtils.isEmpty(savedUsername) && !TextUtils.isEmpty(savedPassword)); } ``` 通过上述步骤,就可以实现在Android Studio中登录界面记住密码的功能。当用户选择记住密码后,下次打开应用时会直接显示之前保存的用户名和密码,并且可以直接进行登录操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值