Android developer must know-the right posture for using View.setid

Source: Internet
Author: User

These two days in writing a histogram of the custom control, with the direct inheritance of viewgroup implementation of the way, we all know that this is a custom control inside the simplest kind of, have time to write a summary share. Here I would like to say that the point is that in writing this custom control encountered a small problem on the View.setid (), on-line check a lot, there are a lot of answers is purely nonsense, finally in StackOverflow found a piecemeal solution, StackOverflow is really a good place to find solutions to almost every problem. Make a note of the problem here for reference.

In fact, this problem should also be a developer must know in advance, because we in the development, we need to dynamically add view in the code and set the ID for it, in this case, how can we avoid duplication with the existing control ID? After the experiment, it is found that the number of an int type directly as a parameter of SetID () will be reported in Androidstudio, saying the warning may be more appropriate because it can be run, and in eclipse this use will not prompt the error, there is no warning. However, regardless of the error or not, this is not safe, there is an ID conflict in the control of the hidden danger.

  

There are no errors or warnings under Eclipse.

Androidstudio the error.

Rearrange the problem description: in Androidstudio, when adding a control dynamically with code, assigning an ID to a control by View.setid (int id) directly assigns a parameter of type int "error", and the correct use posture of View.setid () is described below.

• Scenario One: Call View.generateviewid () as the parameter of the SetID, but this scenario is not the best solution because the View.generateviewid () method must be the SDK version 17 and above, or an error. (but there are also can be solved by self-writing a utils.generateviewid (), but since there is a better way to plan two, not too much to repeat this method)

My_view.setid (View.generateviewid ());

• Scenario Two: Add Ids.xml (the name is optional) file under Res/values/, the code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <item name= "My_view" type= "id"/></ Resources>

Then make the following settings in your code:

My_view.setid (R.id.my_view);

Android developer must know-the right posture for using View.setid

Related Article

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.