这是我的情况。 我定义了一个xml文件,其中包含2个表格布局,每个布局有1个表格行,每行有3个相对布局,每个相对布局有3个图像视图。
当我点击它时,我想降低1相对布局的所有图像视图上的alpha,所以我使用这段代码:
for(int i = 0;i<randStupac.getChildCount();i++)
{
ImageView image =(ImageView)randStupac.getChildAt(i);
image.setAlpha(100);
}
randStupac是一个包含3个imageViews的相对布局,但由于某些原因,这不仅会改变那些3个ImageView,还会随机改变来自其他相对布局的其他图像,我不希望这样。
我用logcat检查了childcount为3。
所以这意味着在3次迭代中没有3次,但是ImageViews中3位以上的随机数发生了变化。
那么ony1 plz能帮助我改变我需要改变的图像吗?
这是xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:paddingBottom="10dp"
android:paddingTop="50dp">
<TableLayout android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/TableLayoutTijelo">
<TableRow android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginLeft="120dp"
android:id="@+id/TableRowTijelo">
<RelativeLayout android:layout_width="wrap_content"
android:id="@+id/StupacTijelo1" android:layout_height="wrap_content"
android:onClick="clickTijelo">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/SlikaTijelo1" android:src="@drawable/tresnja"></ImageView>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@+id/SlikaTijelo1" android:id="@+id/SlikaTijelo4" android:src="@drawable/tresnja"></ImageView>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@+id/SlikaTijelo4"
android:id="@+id/SlikaTijelo7" android:src="@drawable/tresnja"></ImageView>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content"
android:id="@+id/StupacTijelo2" android:layout_height="wrap_content"
android:onClick="clickTijelo">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/SlikaTijelo2"
android:src="@drawable/jabuka"></ImageView>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@+id/SlikaTijelo2" android:id="@+id/SlikaTijelo5" android:src="@drawable/lubenica"></ImageView>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@+id/SlikaTijelo5"
android:id="@+id/SlikaTijelo8" android:src="@drawable/tresnja"></ImageView>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content"
android:id="@+id/StupacTijelo3" android:layout_height="wrap_content"
android:onClick="clickTijelo">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/SlikaTijelo3" android:src="@drawable/lubenica"></ImageView>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@+id/SlikaTijelo3"
android:id="@+id/SlikaTijelo6" android:src="@drawable/ribizli"></ImageView>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@+id/SlikaTijelo6"
android:id="@+id/SlikaTijelo9" android:src="@drawable/lubenica"></ImageView>
</RelativeLayout>
</TableRow>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/brojacBod"
android:text="9999999" android:layout_marginTop="50dp"
android:gravity="center" android:layout_marginRight="250dp"
android:layout_marginLeft="7dp"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="9999999"
android:id="@+id/brojacPokusaj" android:layout_marginTop="110dp"
android:layout_marginLeft="35dp"></TextView>
</TableLayout>
<TableLayout android:layout_height="wrap_content"
android:layout_alignParentBottom="true" android:layout_width="fill_parent"
android:id="@+id/TableLayoutRandom">
<TableRow android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginLeft="120dp"
android:id="@+id/TableRowRandom">
<RelativeLayout android:layout_width="wrap_content"
android:id="@+id/StupacRandom1" android:layout_height="wrap_content"
android:onClick="clickRandom">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/icon"
android:id="@+id/SlikaRandom1"></ImageView>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/icon"
android:layout_below="@+id/SlikaRandom1" android:id="@+id/SlikaRandom4"></ImageView>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/icon"
android:layout_below="@+id/SlikaRandom4" android:id="@+id/SlikaRandom7"></ImageView>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content"
android:id="@+id/StupacRandom2" android:layout_height="wrap_content"
android:onClick="clickRandom">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/SlikaRandom2" android:src="@drawable/icon"></ImageView>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/icon"
android:layout_below="@+id/SlikaRandom2" android:id="@+id/SlikaRandom5"></ImageView>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/icon"
android:layout_below="@+id/SlikaRandom5" android:id="@+id/SlikaRandom8"></ImageView>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content"
android:id="@+id/StupacRandom3" android:layout_height="wrap_content"
android:onClick="clickRandom">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/icon"
android:id="@+id/SlikaRandom3"></ImageView>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/icon"
android:layout_below="@+id/SlikaRandom3" android:id="@+id/SlikaRandom6"></ImageView>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/icon"
android:layout_below="@+id/SlikaRandom6" android:id="@+id/SlikaRandom9"></ImageView>
</RelativeLayout>
</TableRow>
<LinearLayout android:id="@+id/linearLayout1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></LinearLayout>
</TableLayout>
</RelativeLayout>
<ImageView android:layout_width="wrap_content" android:src="@drawable/icon"
android:layout_height="wrap_content" android:layout_marginTop="75dp"
android:id="@+id/comboDouble"></ImageView>
<ImageView android:layout_height="wrap_content"
android:layout_width="wrap_content" android:src="@drawable/icon"
android:layout_marginTop="120dp" android:id="@+id/comboMulti"></ImageView>
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/ReklamaIgra">
</RelativeLayout>
这是“点击”功能:;
public void clickRandom(final View view)
{
if(odabrano == true)
{
switch(stupacRandom)
{
case 1:randStupac = (RelativeLayout)findViewById(R.id.StupacRandom1);break;
case 2:randStupac = (RelativeLayout)findViewById(R.id.StupacRandom2);break;
case 3:randStupac = (RelativeLayout)findViewById(R.id.StupacRandom3);break;
}
randStupac.startAnimation(animacijaDeselect);
for(int i = 0;i<randStupac.getChildCount();i++)
{
ImageView slika1 =(ImageView)randStupac.getChildAt(i);
slika1.setAlpha(puno);
}
}
switch(view.getId())
{
case R.id.StupacRandom1 :
{
stupacRandom = 1;
randStupac = (RelativeLayout)findViewById(R.id.StupacRandom1);
break;
}
case R.id.StupacRandom2 :
{
stupacRandom = 2;
randStupac = (RelativeLayout)findViewById(R.id.StupacRandom2);
break;
}
case R.id.StupacRandom3 :
{
stupacRandom = 3;
randStupac = (RelativeLayout)findViewById(R.id.StupacRandom3);
break;
}
}
在我编写函数之前,我知道,为整个类声明RelativeLayout randStupac。
很抱歉,我写的字是克罗地亚语
答案 0 :(得分:0)
删除所有代码并添加此
public void clickRandom(final View view) {
for(int i = 0; i < view.getChildCount(); i++) {
ImageView slika1 = (ImageView) view.getChildAt(i);
slika1.setAlpha(100);
}
}
或者你可以为所有ImagesViews使用相同的id(我的意思是imageView1,imageView2和imageView3)
<RelativeLayout android:layout_width="wrap_content"
android:id="@+id/StupacRandom3"
android:layout_height="wrap_content"
android:onClick="clickRandom">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:id="@+id/imageView1" />
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_below="@+id/imageView2"
android:id="@+id/SlikaRandom6" />
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_below="@+id/imageView3"
android:id="@+id/SlikaRandom9" />
</RelativeLayout>
并写下这样的东西
public void clickRandom(final View view) {
ImageView slika1 = (ImageView) view.findViewById(R.id.imageView1);
slika1.setAlpha(100);
ImageView slika2 = (ImageView) view.findViewById(R.id.imageView2);
slika2.setAlpha(100);
ImageView slika3 = (ImageView) view.findViewById(R.id.imageView3);
slika3.setAlpha(100);
}