我需要从 for 中实例化和修改视图。是否可以基于 i 获取其中的 ID?
I need to instantiate and modify views from a for. Is it possible to get the ids in that for base on the i?
我有以下线性布局:
<?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 中使用它们,例如:
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
经典的Android问题,至少对我来说是这样。据我所知,您想要实现的目标在语法上是不可能的,但还有其他方法。您至少有 2 个选项。
在代码中动态创建视图。这看起来像下面这样:
LinearLayout layout = new LinearLayout(ArmedResponseDetailsActivity.this);
TableLayout.LayoutParams layoutParams = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layout.setLayoutParams(layoutParams);
for (int i = 0; i < 10; i++) {
View v = new View(ArmedResponseDetailsActivity.this);
TableLayout.LayoutParams viewParams = new TableLayout.LayoutParams(8, 30);
layoutParams.rightMargin = 5;
v.setLayoutParams(viewParams);
v.setBackground(R.drawable.white_mic_value);
layout.addView(v);
}
使用 ListView along with an ArrayAdapter.
更专业和首选的方法是#2。再一次,看来您可能正在使用图像。如果是这样,您可以查看 Glide.
这样的库
我是这样做的:
int[] micIDS = new int[] {R.id.white1, R.id.white2, R.id.white3, R.id.white4, R.id.white5, R.id.white6, R.id.white7, R.id.white8, R.id.white9, R.id.white10, R.id.white11, R.id.white12, R.id.white13, R.id.white14, R.id.white15, R.id.white16 };
for(int i= 1 ; i < 17; i++){
View view = rootView.findViewById(micIDS[i-1]);
Log.i("","view background: " + view.getBackground().toString());
if(amplitude > i*viewThreshold) {
view.setBackgroundResource(R.drawable.white_mic_value);
}else{
view.setBackgroundResource(R.drawable.half_white_mic_value);
}
}
我有以下线性布局:
<?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 中使用它们,例如:
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
经典的Android问题,至少对我来说是这样。据我所知,您想要实现的目标在语法上是不可能的,但还有其他方法。您至少有 2 个选项。
在代码中动态创建视图。这看起来像下面这样:
LinearLayout layout = new LinearLayout(ArmedResponseDetailsActivity.this); TableLayout.LayoutParams layoutParams = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); layout.setLayoutParams(layoutParams); for (int i = 0; i < 10; i++) { View v = new View(ArmedResponseDetailsActivity.this); TableLayout.LayoutParams viewParams = new TableLayout.LayoutParams(8, 30); layoutParams.rightMargin = 5; v.setLayoutParams(viewParams); v.setBackground(R.drawable.white_mic_value); layout.addView(v); }
使用 ListView along with an ArrayAdapter.
更专业和首选的方法是#2。再一次,看来您可能正在使用图像。如果是这样,您可以查看 Glide.
这样的库我是这样做的:
int[] micIDS = new int[] {R.id.white1, R.id.white2, R.id.white3, R.id.white4, R.id.white5, R.id.white6, R.id.white7, R.id.white8, R.id.white9, R.id.white10, R.id.white11, R.id.white12, R.id.white13, R.id.white14, R.id.white15, R.id.white16 };
for(int i= 1 ; i < 17; i++){
View view = rootView.findViewById(micIDS[i-1]);
Log.i("","view background: " + view.getBackground().toString());
if(amplitude > i*viewThreshold) {
view.setBackgroundResource(R.drawable.white_mic_value);
}else{
view.setBackgroundResource(R.drawable.half_white_mic_value);
}
}