我有以下的线性layour:我需要实例化和修改for的视图。是否有可能获得基于我的ID?
<?xml version="1.0" encoding="utf-8"?>
<View
android:id="@+id/white1"
android:layout_width="8dp"
android:layout_height="30dp"
android:layout_marginRight="5dp"
android:background="@drawable/white_mic_value"/>
<View
android:id="@+id/white2"
android:layout_width="8dp"
android:layout_height="30dp"
android:layout_marginRight="5dp"
android:background="@drawable/white_mic_value"/>
<View
android:id="@+id/white3"
android:layout_width="8dp"
android:layout_height="30dp"
android:layout_marginRight="5dp"
android:background="@drawable/white_mic_value"/>
<View
android:id="@+id/white4"
android:layout_width="8dp"
android:layout_height="30dp"
android:layout_marginRight="5dp"
android:background="@drawable/white_mic_value"/>
<View
android:id="@+id/white5"
android:layout_width="8dp"
android:layout_height="30dp"
android:layout_marginRight="5dp"
android:background="@drawable/white_mic_value"/>
<View
android:id="@+id/white6"
android:layout_width="8dp"
android:layout_height="30dp"
android:layout_marginRight="5dp"
android:background="@drawable/white_mic_value"/>
<View
android:id="@+id/white7"
android:layout_width="8dp"
android:layout_height="30dp"
android:layout_marginRight="5dp"
android:background="@drawable/white_mic_value"/>
<View
android:id="@+id/white8"
android:layout_width="8dp"
android:layout_height="30dp"
android:layout_marginRight="5dp"
android:background="@drawable/white_mic_value"/>
<View
android:id="@+id/white9"
android:layout_width="8dp"
android:layout_height="30dp"
android:layout_marginRight="5dp"
android:background="@drawable/white_mic_value"/>
<View
android:id="@+id/white10"
android:layout_width="8dp"
android:layout_height="30dp"
android:layout_marginRight="5dp"
android:background="@drawable/white_mic_value"/>
<View
android:id="@+id/white11"
android:layout_width="8dp"
android:layout_height="30dp"
android:layout_marginRight="5dp"
android:background="@drawable/half_white_mic_value"/>
<View
android:id="@+id/white12"
android:layout_width="8dp"
android:layout_height="30dp"
android:layout_marginRight="5dp"
android:background="@drawable/half_white_mic_value"/>
<View
android:id="@+id/white13"
android:layout_width="8dp"
android:layout_height="30dp"
android:layout_marginRight="5dp"
android:background="@drawable/half_white_mic_value"/>
<View
android:id="@+id/white14"
android:layout_width="8dp"
android:layout_height="30dp"
android:layout_marginRight="5dp"
android:background="@drawable/half_white_mic_value"/>
<View
android:id="@+id/white15"
android:layout_width="8dp"
android:layout_height="30dp"
android:layout_marginRight="5dp"
android:background="@drawable/half_white_mic_value"/>
<View
android:id="@+id/white16"
android:layout_width="8dp"
android:layout_height="30dp"
android:layout_marginRight="5dp"
android:background="@drawable/half_white_mic_value"/>
,我想在使用他们的,是这样的:
for(int i= 1 ; i < 17; i++){
if(soundMeter.getAmplitude() > i*viewThreshold) {
rootView.findViewById(R.id.white+).setBackgroundResource(R.drawable.white_mic_value);
}else{
rootView.findViewById(R.id.white+i).setBackgroundResource(R.drawable.half_white_mic_value);
}
}
我尝试: int id = getResources().getIdentifier("white" + i, "view", getActivity().getPackageName());
但它崩溃,出现以下错误:
06-20 09:12:45.011: E/InputEventSender(28432): Exception dispatching finished signal.
06-20 09:12:45.011: E/MessageQueue-JNI(28432): Exception in MessageQueue callback: handleReceiveCallback
06-20 09:12:45.031: E/MessageQueue-JNI(28432): java.lang.NullPointerException
我只是需要做一个麦克风的幅度来看,与此类似: https://s3.amazonaws.com/uploads.hipchat.com/39260/829560 /3tVsu9UkFrDlarJ/upload.png 我正在考虑用逻辑来动态创建它们,但我偏好了一个xml,需要固定数量的元素,并且使用了butterknife,它会注入它们,所以它更快。所以我只是把它们留在了xml中,并且使用了一系列整数:D –