我在两个选项卡中具有带有公共标题的tablayout,并且我想更新公共标题中的Button

时间:2019-01-28 10:59:54

标签: android android-studio android-layout button uibutton

我有一个带有5个标签的TabLayout,其中两个标签具有相同的页眉布局,并且此布局包含在tabfragment布局中。  我的header_layout中有一个button,我想每10秒更改/更新一次按钮图片,但是wifiIcon.setBackgroundResource(R.drawable.wifiexcellent);不更改图片。

   LayoutInflater inflater = (LayoutInflater)   getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View viewHeader = inflater.inflate(R.layout.header_layout, null);
    setWifiIconOnHeader(viewHeader);

 protected void setWifiIconOnHeader(View myView) {
    Button        wifiIcon    =  (Button) myView.findViewById(R.id.wifiIcon);
    wifiIcon.setBackgroundResource(R.drawable.wifiexcellent);
    if (wifiIcon!=null) {
        WifiIndicatorObj = new WifiIndicator(getApplicationContext(), wifiIcon);
    }
}

我该如何更改wifiIcon的Button图像,在调试时该图像正在更改但在UI中未更改,我是否需要刷新或我到底在做什么错?

public static void setWifiIcon(Context context, Button wifiIcon) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo Info = cm.getActiveNetworkInfo();
    if (Info == null || !Info.isConnectedOrConnecting()) {
        wifiIcon.setBackgroundResource(R.drawable.wifinosignal);
        // Log.i("WIFI CONNECTION", "No connection");
    } else {
        int netType = Info.getType();
        if (netType == ConnectivityManager.TYPE_WIFI) {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            if(wifiManager.isWifiEnabled()){
                if(wifiManager.getWifiState()==WifiManager.WIFI_STATE_ENABLED){
                    //Wifi is enabled
                    int linkSpeed = wifiManager.getConnectionInfo().getLinkSpeed();
                    int rssi = wifiManager.getConnectionInfo().getRssi();
                    //Log.i("WIFI CONNECTION", "Wifi Timer by :"+context.getPackageName()+"Wifi connection speed: "+linkSpeed + " rssi: "+rssi);
                    int normalizedRSSI= normalizeRssi(rssi);
                    if(normalizedRSSI>80){
                        //wIFI Level Very High
                        wifiIcon.setVisibility(View.INVISIBLE);
                        wifiIcon.setBackgroundResource(R.drawable.wifiexcellent);
                        return;
                    }
                    else if (normalizedRSSI>60){
                        //wIFI Level High
                        wifiIcon.setBackgroundResource(R.drawable.wifiverygood);
                        return;

                    }
                    else if (normalizedRSSI>40){
                        //wIFI Level Medium
                        wifiIcon.setBackgroundResource(R.drawable.wifigood);
                        return;

                    }
                    else if (normalizedRSSI>20){
                        ////wIFI Level Low
                        wifiIcon.setBackgroundResource(R.drawable.wifilow);
                        return;

                    }
                    else if (normalizedRSSI>10){
                        ////wIFI Level Very Low
                        wifiIcon.setBackgroundResource(R.drawable.wifiverylow);
                        return;

                    }
                    else{
                        wifiIcon.setBackgroundResource(R.drawable.wifiverylow);
                        return;
                    }

                }
            }
        }
    }
}

0 个答案:

没有答案