我有一个LinearLayout
通过Run_Time动态声明,虽然我没有固定的名称或标签来定义该布局,虽然我试图为wach分配一个标签取决于一些变量和稍后通过该变量检索它,如:
layout.setTag(index , "something"); // "index" is a variable it's value obtained through the run_time
但我得到了这个错误:
ERROR/AndroidRuntime(643): Caused by: java.lang.IllegalArgumentException: The key must be an application-specific resource id.
并且在搜索之后我发现问题是我必须为该标签分配固定资源但是我将无法区分不同的布局,那么有没有办法达到我的方法?
答案 0 :(得分:0)
你无法使用setTag(int,Object)的原因是因为android在'int'参数中需要预先编译的唯一id。
一种方法是:
在String文件中声明您的标记,如下所示:
<string name="FirstTag">1</string>
<string name="SecondTag">2</string>
并设置使用:
layout.setTag(R.string.FirstTag, "something");
获得价值:
layout.getTag(R.string.FirstTag).toString();
另一种方式是创建一个名为values / tags.xml的新文件并写:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<item name="FirstTag" type="id">1</item>
</resources>
或您也可以使用R.id.*
作为标记ID,我认为您可以解决问题
答案 1 :(得分:0)
检查一下: (使用标签)
Java代码:
public class DynamicTetView extends Activity
{
RelativeLayout rl1;
Button b1;
TextView txt,txt3;
int i = 0;
HashMap<String, Integer> map = new HashMap<String, Integer>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dynamic_tet_view);
rl1 = (RelativeLayout)findViewById(R.id.rl1);
b1 = (Button)findViewById(R.id.button1);
RelativeLayout.LayoutParams param2 = new RelativeLayout.
LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
param2.setMargins(5, 0, 0, 5);
txt3 = new TextView(this);
txt3.setId(500);
Log.d("Id of txt3........", ""+txt3.getId());
txt3.setText("Hii");
rl1.addView(txt3, param2);
for(i=0;i<10;i++)
{
RelativeLayout.LayoutParams param = new RelativeLayout.
LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
param.setMargins(5, 0, 0, 5);
txt = new TextView(this);
txt.setId(i+1);
txt.setTag(i+1);
Log.d("Id........", ""+txt.getId());
map.put("hi"+txt.getId(), i);
if(txt.getId()!=1)
{
param.addRule(RelativeLayout.BELOW,(txt.getId()-1));
}
else
{
param.addRule(RelativeLayout.BELOW,txt3.getId());
}
txt.setText("Text"+(i+1));
rl1.addView(txt,param);
txt.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
String tag = v.getTag().toString();
Toast.makeText(getBaseContext(),"You clicked "+tag, Toast.LENGTH_LONG).show();
}
});
}
b1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
rl1.removeAllViews();
}
});
}
}
<强> XML:强>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DynamicTetView" >
<RelativeLayout
android:id="@+id/rl1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="48dp"
android:text="Clear" />
</RelativeLayout>
</RelativeLayout>