Android layout 当以编程方式创建视图ID时,检索人类可读视图ID的正确调用是什么?
很抱歉标题太糟糕了 我正在尝试获取我在代码中创建的一些视图的可读ID。这是在拖放位代码中完成的 以下是设置视图的代码:Android layout 当以编程方式创建视图ID时,检索人类可读视图ID的正确调用是什么?,android-layout,drag-and-drop,Android Layout,Drag And Drop,很抱歉标题太糟糕了 我正在尝试获取我在代码中创建的一些视图的可读ID。这是在拖放位代码中完成的 以下是设置视图的代码: //In onCreate: LinearLayout s5=((LinearLayout) findViewById(R.id.stack4)); s5.addView(topCardbuild()); s5.addView(faceCardbuild()); //topcardbuild is the same, but with a differen
//In onCreate:
LinearLayout s5=((LinearLayout) findViewById(R.id.stack4));
s5.addView(topCardbuild());
s5.addView(faceCardbuild());
//topcardbuild is the same, but with a different drawable passed to it.
public View faceCardbuild(){
LinearLayout btnFace = new LinearLayout(this);
LinearLayout.LayoutParams paramsHI = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
// button margins
paramsHI.setMargins(0, 0, 0, -60);
//paramsLO.setMargins() left top right botton
// button height/width *pixels*
paramsHI.height = 96;
paramsHI.width = 72;
btnFace.setOrientation(LinearLayout.VERTICAL);
ImageButton i1 = new ImageButton(this);
i1.setImageResource(R.drawable.c6);
//getResources().getResourceEntryName("ic_launcher", "drawable", getPackageName());
int resID;
//resID = getResources().getResourceName("ic_launcher", "drawable", getPackageName());
resID = getResources().getIdentifier("c6", "drawable", getPackageName());
i1.setId(resID);
Log.d("facecard build", "Facecard resID is " + resID + " image resource ID is " + R.drawable.c6);
Log.d("facecard build", " converted that is " + getResources().getResourceEntryName(resID));
btnFace.addView(i1, paramsHI);
i1.setOnTouchListener(new MyTouchListener());
return btnFace;
}
那些日志正确地向我报告了姓名。但是,当我转到拖放代码并尝试检索名称时,它会出错并崩溃。如果我不尝试检索它们,我可以毫无问题地拖放它们
这是我的拖放代码:
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
View view = (View) event.getLocalState();
//ViewGroup owner = (ViewGroup) view.getParent();
//It doesn't matter if it's a viewgroup or a linear layout. Both crash.
LinearLayout owner = (LinearLayout) view.getParent();
Log.d("drop info", "Linear Layout owner is " + getResources().getResourceEntryName(owner.getId()));
Log.d("drop info", "Sending card is " + getResources().getResourceEntryName(view.getId()));
LinearLayout container = (LinearLayout) v;
Log.d("drop info", "container is " + getResources().getResourceEntryName((container.getId())) + " it has children " + container.getChildCount() + " before move");
if (viewCheck(view, container)){
//returns true, move views
owner.removeView(view);
container.addView(view);
view.setVisibility(View.VISIBLE);
}else{
//returns false, don't move views
}
if (owner.getChildCount() < 2){
owner.getLayoutParams().height = 160;
}else {
owner.getLayoutParams().height = LinearLayout.LayoutParams.WRAP_CONTENT;
}
Log.d("drop info", " and children after move " + container.getChildCount());
cCount = container.getChildCount() - 2;
View temp;
if (cCount >= 1) {
temp = container.getChildAt(cCount);
String child1;
child1 = getResources().getResourceName(temp.getId());
Log.d("drop info", "one less child is " + child1);
Log.d("drop info", "sending stack has children " + owner.getChildCount());
}
if (container.getChildCount()>= 2){
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 0, -60);
view.setLayoutParams(lp);
}
if (container.getChildCount() < 2){
owner.getLayoutParams().height = 160;
}else {
container.getLayoutParams().height = LinearLayout.LayoutParams.WRAP_CONTENT;
}
break;
拖放代码适用于我在XML文件中创建的ImageButtonSView。我在代码中创建的视图在检索ID时遇到问题
这是我在视图操作方面所做过的最广泛的研究,所以如果有明显的错误,请小心
编辑:这是我在尝试读取ID时遇到的错误。
W/资源类型﹕ 获取资源号0xFFFFFF的名称时没有已知的包
谷歌的每一个结果都表明,他们要么自己解决了这个问题,要么说要明确定义ID。据我所知,我已经在faceCardbuild中做到了这一点 我做了一个小函数来简单地检查视图的名称,发现它正在检索-1 在花了一段时间查看代码之后,我看到我将imagebutton分配给了i1 setID函数,但不是它的父函数btnFace 我更改了它,使btnFace.setID得到了我的resID值,它成功了 我现在可以检索这些视图的ID