我有ImageView数组,但是当我在java中设置图像时它不起作用,它使用的是在xml布局中定义的默认图像
private ImageView[] imageView=new ImageView[2];
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.change_selection_activity);
for(int i=0;i<2;i++)
imageView[i]=new ImageView(this);
imageView[0]= (ImageView) findViewById(R.id.selection_one);
imageView[1]= (ImageView) findViewById(R.id.selection_two);
imageView[0].setImageResource(R.drawable.ic_network_wifi_black_48dp);
imageView[1].setImageResource(R.drawable.ic_network_wifi_black_48dp);
}
xml文件:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#96000000"
>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="16dp"
android:gravity="center"
>
<ImageView
android:layout_width="@dimen/bubble_dimension"
android:layout_height="@dimen/bubble_dimension"
android:src="@drawable/ic_info_black_48dp"
android:padding="8dp"
android:layout_marginRight="16dp"
android:id="@+id/selection_one" />
<ImageView
android:layout_width="@dimen/bubble_dimension"
android:layout_height="@dimen/bubble_dimension"
android:padding="8dp"
android:layout_marginRight="16dp"
android:id="@+id/selection_two"
android:src="@drawable/ic_info_black_48dp"/>
<ImageView
android:layout_width="@dimen/bubble_dimension"
android:layout_height="@dimen/bubble_dimension"
android:padding="8dp"
android:scaleType="centerCrop"
android:id="@+id/selection_three"
android:src="@drawable/ic_info_black_48dp"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="16dp"
android:gravity="center">
<ImageView
android:layout_width="@dimen/bubble_dimension"
android:layout_height="@dimen/bubble_dimension"
android:padding="8dp"
android:layout_marginRight="16dp"
android:id="@+id/selection_four"
android:src="@drawable/ic_info_black_48dp"/>
<ImageView
android:layout_width="@dimen/bubble_dimension"
android:layout_height="@dimen/bubble_dimension"
android:padding="8dp"
android:layout_marginRight="16dp"
android:id="@+id/selection_five"
android:src="@drawable/ic_info_black_48dp"/>
<ImageView
android:layout_width="@dimen/bubble_dimension"
android:layout_height="@dimen/bubble_dimension"
android:padding="8dp"
android:id="@+id/selection_six"
android:src="@drawable/ic_info_black_48dp"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:layout_width="@dimen/bubble_dimension"
android:layout_height="@dimen/bubble_dimension"
android:padding="8dp"
android:layout_marginRight="16dp"
android:id="@+id/selection_seven"
android:src="@drawable/ic_info_black_48dp"
/>
<ImageView
android:layout_width="@dimen/bubble_dimension"
android:layout_height="@dimen/bubble_dimension"
android:padding="8dp"
android:layout_marginRight="16dp"
android:id="@+id/selection_eight"
android:src="@drawable/ic_info_black_48dp"/>
<ImageView
android:layout_width="@dimen/bubble_dimension"
android:layout_height="@dimen/bubble_dimension"
android:padding="8dp"
android:id="@+id/selection_nine"
android:src="@drawable/ic_info_black_48dp"/>
</TableRow>
</TableLayout>
请帮忙,当我使用没有数组的ImageView时,它正常工作
PS:onClicklistener在这些imageView [i]
上完美运行答案 0 :(得分:0)
试试这个:
Resources resources = getResources();
imageView[0].setImageDrawable(resources.getDrawable(R.drawable.ic_network_wifi_black_48dp));
imageView[1].setImageDrawable(resources.getDrawable(R.drawable.ic_network_wifi_black_48dp));