如何设置dynamic创build的布局的ID?

我想在编程创build的布局中给ID一些视图(textview,imageview等)。 那么设置ID的最好方法是什么?

你创build一个ids.xml文件,并把所有你需要的id放在里面

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

现在为您的dynamic创build的布局或视图,您可以使用这些id如下

 new_layout1.setId(R.id.layout1); new_view2.setId(R.id.layout2); new_layout3.setId(R.id.layout3); 

我希望它可以帮助你。

创build文件夹res/values/ids.xml

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

在这个Activity类中调用

 ImageView refreshImg = new ImageView(activity); ImageView settingsImg = new ImageView(activity); refreshImg.setId(R.id.refresh); settingsImg .setId(R.id.settings); 
 int id=100+1; layout.setId(id); 

Google终于意识到需要为编程创build的视图生成唯一的ID …

从API级别17和以上,一旦可以调用View.generateViewId()

如果您将一组组件以编程方式重复放置到布局中,如下所示:

 <LinearLayout> <ImageView> <TextView> <Button> <ImageView> <TextView> <Button> <ImageView> <TextView> <Button> ... </LinearLayout> 

那么,你可以使用for循环,并相应地给予ID:

 for(int i=0;i<totalGroups;i++) { ImageView img; TextView tv; Button b; ... // set other properties of above components img.setId(i); tv.setId(i); b.setId(i); ... //handle all events on these components here only ... //add all components to your main layout } 

或者,如果只需要添加一组组件,则可以使用任何较大的整数,并且不会与其他组件的ID在资源中发生冲突。不会有太大的冲突。

试试这个代码! 这应该有助于提出一个想法。

activity_prac_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:text="@string/edit_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/display_txt" android:textStyle="bold" android:textSize="18sp" android:textAlignment="center" android:layout_gravity="center_horizontal"/> <GridLayout android:id="@+id/my_grid" android:layout_width="match_parent" android:layout_height="wrap_content" android:rowCount="4"> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/linear_view"> <Button android:text="@string/button_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/my_btn" android:layout_gravity="center_horizontal"/> <TextView android:text="@string/edit_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/my_txt" android:textSize="18sp" /> </LinearLayout> </GridLayout> </LinearLayout> 

这是代码的其余部分

  public class AnotherActivity extends AppCompatActivity { private int count = 1; List<Integer> gridArray; private TextView myDisplayText; @Override protected void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); gridArray = new ArrayList<>(); gridArray.add(Integer.valueOf(1)); setContentView(R.layout.activity_prac_main); findViews(); } private void findViews(){ GridLayout gridLayout = (GridLayout)findViewById(R.id.my_grid); gridLayout.setColumnCount(4); LinearLayout linearLayout = (LinearLayout) gridLayout.findViewById(R.id.linear_view); linearLayout.setTag("1"); Button myButton = (Button) linearLayout.findViewById(R.id.my_btn); myButton.setTag("1"); TextView myText = (TextView) linearLayout.findViewById(R.id.my_txt); myText.setText("1"); myDisplayText = (TextView) findViewById(R.id.display_txt); myButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { TextView txt = (TextView) view; myDisplayText.setText("PRESS " + txt.getTag().toString()); if(count < 24) { createView(); } else{ dialogBox(); } } }); } private void createView(){ LinearLayout ll = new LinearLayout(this); ll.setId(Integer.valueOf(R.id.new_view_id)); ll.setTag(String.valueOf(count+1)); Button newBtn = createButton(); newBtn.setId(Integer.valueOf(R.id.new_btn_id)); newBtn.setTag(String.valueOf(count+1)); TextView txtView = createText(); txtView.setId(Integer.valueOf(R.id.new_txt_id)); txtView.setTag(String.valueOf(count+1)); txtView.setText(String.valueOf(count+1)); GridLayout gridLayout = (GridLayout)findViewById(R.id.my_grid); ll.addView(newBtn); ll.addView(txtView); ll.setOrientation(LinearLayout.VERTICAL); gridLayout.addView(ll); count++; } private Button createButton(){ Button myBtn = new Button(this); myBtn.setText(R.string.button_send); myBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { TextView txt = (TextView) view; myDisplayText.setText("PRESS " + txt.getTag().toString()); if(count < 24) { createView(); } else{ dialogBox(); } } }); return myBtn; } public void dialogBox() { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setMessage("GRID IS FULL!"); alertDialogBuilder.setPositiveButton("DELETE", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { GridLayout gridLayout = (GridLayout)findViewById(R.id.my_grid); gridLayout.removeAllViews(); count = 0; createView(); } }); alertDialogBuilder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { } }); AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show(); } private TextView createText(){ TextView myTxt = new TextView(this); myTxt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18); return myTxt; } } 

正如你可以看到ids是在res – > values – > ids文件中创build的。

在dynamic创build视图时,视图的id是相同的。

每个TextView共享相同的ID。 每个button共享相同的ID。 每个布局共享相同的ID。

ID只是访问视图的内容很重要。

然而,标签是使每个视图彼此独一无二的。

希望这可以帮助你!

您可以将您的ID定义为资源,然后使用视图的setId()进行设置。 在一个xml文件中,定义ID如下所示:

 <resources> <item type="id">your id name</item> </resources> 

那么,在java文件中使用..

 layout.setId(R.id.<your id name>)