简体   繁体   中英

How to generate ID i created in java class to r.java

im new to android programming.

im wondering if it is possible to generate ID i created in java class to r.java

The ID i want to generate is nBtnLayoutlist.

public class classABC extends Activity {
    private int nBtnLayoutlist = 20;

    private OnClickListener onClick() {
        return new OnClickListener() {

            public void onClick(View v) {

            nBtnLayout = (LinearLayout)findViewById(R.id.nBtnLayoutList);

            }
        };
    }

    private LinearLayout newBtnLayout(){
    LinearLayout nBtnLayout = new LinearLayout(this);
    final LayoutParams lparams = new LayoutParams(LayoutParams.MATCH_PARENT,       LayoutParams.WRAP_CONTENT);
    nBtnLayout.setOrientation(LinearLayout.VERTICAL);
    nBtnLayout.setLayoutParams(lparams); 
    nBtnLayout.setWeightSum(100);
    nBtnLayout.setId(nBtnLayoutList);
    return nBtnLayout;

}

The ids in R.java are generated at build time from the contents of your .xml files. R.java cannot be modified by hand and it cannot be modified at run time.

If you want, you can define an id resource by declaring it in some file in res/values (the file can have any name):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item
        type="id"
        name="id_name" />
</resources>

You can always set the id of a view in your layout by calling setId(int) (as you are already doing).

EDIT: If you want to use a constant in the call such as

nBtnLayout.setId(nBtnLayoutList);

then you need to use the same constant in calls such as

nBtnLayout = (LinearLayout)findViewById(nBtnLayoutList); // NOT R.id.nBtnLayoutList

- nBtnLayoutList is a reference to the LinearLayout.

- You can set id to the LinearLayout by passing integer in setId() method.

Eg:

nBtnLayout.setId(1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM