Android 如何将字符串id分配给UI元素。
我正在android上开发一个应用程序,并在一个循环中生成UI元素。但是我需要这些元素有一个带有字母和数字的id,例如Android 如何将字符串id分配给UI元素。,android,android-layout,android-ui,Android,Android Layout,Android Ui,我正在android上开发一个应用程序,并在一个循环中生成UI元素。但是我需要这些元素有一个带有字母和数字的id,例如“rl1”或“rl2”。我试图使用方法RelativeLayout.setId(),但该方法只接受int。有没有一种方法可以在不受数字限制的情况下设置我想要的ID 谢谢 这是我正在努力使之生效的代码 for (int i=1; i < 10; i++) { //gets the frameview where the elements will be created
“rl1”
或“rl2”
。我试图使用方法RelativeLayout.setId()
,但该方法只接受int
。有没有一种方法可以在不受数字限制的情况下设置我想要的ID
谢谢
这是我正在努力使之生效的代码
for (int i=1; i < 10; i++)
{
//gets the frameview where the elements will be created.
String LinearLayoutId = "frameview1";
int resID = getResources().getIdentifier(LinearLayoutId, "id", "com.myapp.ERS");
LinearLayout linearLayout = (LinearLayout)findViewById(resID);
//creates the RelativeLayout that will hold the ImageIcon and the TextView
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,40 );
rl.setLayoutParams(lp);
rl.setId("rl"); /// >>>> I would like here to set and ID of "rl1" for example.
rl.setBackgroundDrawable(getResources().getDrawable(R.drawable.bk36));
//creates the image icon within the layout at the left side
ImageView image = new ImageView(this);
lp = new RelativeLayout.LayoutParams(
40,RelativeLayout.LayoutParams.MATCH_PARENT );
image.setLayoutParams(lp);
String imageicon = "icon_"+i;
resID = getResources().getIdentifier(imageicon, "drawable", "com.myapp.ERS");
image.setImageDrawable(getResources().getDrawable(resID)); //sets the icon
rl.addView(image); //adds the ImageView to the relative layout
//creates the TextView within the layout with a 40 margin to the left
TextView tv = new TextView(this);
lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT );
lp.setMargins(40, 0, 0, 0);
tv.setLayoutParams(lp);
String textViewID = "tv"+i;
resID = getResources().getIdentifier(textViewID, "string", "com.myapp.ERS");
tv.setText(getResources().getString(resID));
tv.setTextColor(Color.BLACK);
tv.setTextSize(25);
rl.addView(tv);//adds the TextView to the relative layout
rl.setOnClickListener(mAddListener);
linearLayout.addView(rl);//adds the RelativeLayout to the LinearLayout
}
我已尝试设置数字ID,但当我使用以下工具查找它们时:
String id = getResources().getResourceEntryName(v.getId());
它找不到它们
首先,我将所有这些都放在一个xml
文件中,但它确实很长,因为列表中有大约40个项目,而且我很难去更改所有项目中的一个字母。我想到了在运行时在for循环中生成它们的想法。我正在测试10个,但我不能让它工作
如果我做错了什么,请原谅我,但是我对这一点还不熟悉。如果您想访问您在其他活动的其他地方提供的ID的元素,为什么不尝试使用SharedReferences作为替代方法。您可能会发现返回到XML
布局并使用R类
生成有意义的ID更容易。虽然由于您没有在问题末尾包含您引用的原始xml
文件,因此我只能猜测您遇到的问题。尽管如此,它似乎符合要求,并允许您按照以下思路创建一些内容:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/hellotextview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hi there"/>
R.id.hellotextview
是在构建项目时自动生成的int(在gen/R.java
中),但是当您选择名称时,您可以为它们分配与您和您的项目相关的内容。因此,您可以使用R.id.rl1
和R.id.rl2
,而不是尝试使用您提到的字符串值,例如“rl1”
和R.id.rl2
除了单个UI元素外,您还可以对字符串(在res/values/strings.xml
中)和存储在项目res/
文件夹下的其他资源(如图标、媒体文件等)使用相同的技术。对于字符串,您可以访问它们getString(R.string.some\u name\u由您指定)代码>
有关更多信息,请访问Android开发者网站。我不熟悉这一点,我不知道您所说的共享参考是什么意思。请查看此链接了解如何使用它:@jerisalan如果您查看代码,生成的元素是布局、图像视图和其他UI元素。我不认为SharedReferences是存储这些ID的真正方式。问题是,当我在方法中设置onclicklistener时,我获取单击的textview的ID,然后使用web视图调用活动,并根据单击的textview的ID,它将加载一个特定的本地html文件,该文件只与特定的textview相关。所以,这就是为什么每当我在运行时生成textview时,我都会尝试设置ID。最后,我只是将Id设置为1、2、3、4,然后将该Id发送到web视图。我不知道这是否有意义,但它是这样工作的。
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/hellotextview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hi there"/>
TextView helloText = (TextView) findViewById(R.id.hellotextview);