1
我有兩個ImageView
和一個Button
以xml:兩個一套ImageView的背景Alpha只有一種成功
<ImageView
android:id="@+id/image1"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/backgroud_msg_notify"
android:src="@mipmap/ic_launcher"
app:layout_constraintRight_toLeftOf="@+id/guideline"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp" />
<ImageView
android:id="@+id/image2"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/backgroud_msg_notify"
android:src="@mipmap/ic_launcher"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="@+id/guideline" />
在我的活動,我這樣做:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_image_view);
final ImageView imageView1 = (ImageView) findViewById(R.id.image1);
final ImageView imageView2 = (ImageView) findViewById(R.id.image2);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageView1.getBackground().setAlpha(100);
imageView2.getBackground().setAlpha(100);
}
});
}
但只有imageView1背景alpha發生了變化,當第一次設置imageview2時,只有imageview2背景發生了變化。所以我複製背景文件並分別設置兩個imageview。兩個imageview都改變了,爲什麼?