【发布时间】:2011-08-28 13:25:46
【问题描述】:
这是我的情况。 我定义了一个带有 2 个表格布局的 xml 文件,每个表格布局有 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 个 ImageViews,还会随机更改来自其他相关布局的其他图像,我不希望这样。
我用 logcat 检查了 childcount 是 3。
所以这意味着在 3 次迭代中没有 3 个,而是 ImageViews 超过 3 个的随机数发生了变化。
那么请 ony1 可以帮我只更改我需要更改的图像吗?
这里是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。
对不起,我写的是克罗地亚语
【问题讨论】:
-
在您初始化 randStupac 变量的位置发布您的 layout.xml 文件和代码。
标签: android xml imageview alpha