-1

我有以下线性布局:

<?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
4

2 回答 2

1

经典的Android问题,至少对我来说。据我所知,您试图实现的目标在语法上是不可能的,但还有其他方法。您在这里至少有 2 个选项。

  1. 在代码中动态创建视图。这将类似于以下内容:

    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);
    }
    
  2. 使用ListViewArrayAdapter

更专业和首选的方式是#2。再说一次,您似乎正在使用图像。如果是这样的话,你可以看看像Glide这样的库。

于 2017-06-20T14:25:40.507 回答
0

我是这样做的:

 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);
                                    }
                                }
于 2017-06-20T15:13:51.593 回答